Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My application has a two forms. form1 has 8 labels and an audio element. Form2 has 8 textboxes with a save button. Both forms are opened at the start of Application. After filling Textboxes in form2. And on the click of save button. The text of labels in form1 should be replaced with Content of the textboxes..
C#
Form1 f1= new Form1();
f1.Label1.Text=textbox1.Text;
f1.Label2.Text=textbox2.Text;
f1.Label3.Text=textbox3.Text;
.....
..
...
..
f1.show();

I can't use this code in Form2 because new object of form1 will be created With new audio element from the beginning... I don't want new audio element playing from the first.. So How do i send data from from1 to form2 with creating new object of form and How do i keep audio from restarting every time i send the data from (FORM1) to (FORM2)...
Posted
Updated 4-Nov-12 22:25pm
v3
Comments
TanvirRaihan 5-Nov-12 5:34am    
You can use custom event to solve this issue.

You should have an underlying data structure with methods to change the data. Usually that is called the business logic layer.

It should also declare and fire the appropriate events to inform your UI of any changes.

[Edit]
It's done something like that:
C#
public static class UnderlyingData
{
    // Here, the data is really stored during application lifetime
    private static string _someText = "Initial Value";

    // A publically accessible property to read and change the data
    public static string SomeText
    {
        get { return (_someText); }
        set { _someText = value; }
    }

    // Inform GUI of changes. Use an event for that.
    public static delegate void StringDelegate( string stringTypedParameter);
    public static event StringDelegate SomeTextChanged;

    // Change publically accessible property to fire the event
    public static string SomeText
    {
        get { return (_someText); }
        set
        {
            _someText = value;

            // Fire the event
            StringDelegate eventHandler = SomeTextChanged;
            if( eventHandler != null)
            {
                eventHandler(_someText);
            }
        }
    }
}

public class Form1 : Form
{
    private void TextBox1.TextChanged(object sender, EventArgs e)
    {
        // Pass text change to business layer
        UnderlyingData.SomeText = TextBox1.Text;
    }
}

public class Form2 : Form
{
    private void Form2_Load(object sender, EventArgs e)
    {
        // Subscribe to business layer's data-changed-event
        UnderlyingData.SomeTextChanged += new UnderlyingData.StringDelegate(UnderlyingData_SomeTextChanged);
    }

    private void UnderlyingData_SomeTextChanged(string someText)
    {
        // Do whatever your UI needs to do with the changed data
        Label42.Text = someText;
    }
}


That's the minimal example. Of course, you can use the event in Form1 as well, if there is some other thing that depends on the changed data. Or you can incorporate validation checks in the property's set accessor.
[/Edit]
 
Share this answer
 
v2
Comments
Sharath2790 5-Nov-12 22:49pm    
Any links or examples.. So that i could understand in a more better way...
lukeer 6-Nov-12 1:54am    
I updated my solution.
Timer would be a solution. How to use Timer

Another Example of Timer
 
Share this answer
 
Comments
lukeer 5-Nov-12 7:40am    
And exactly how do you think could a timer be of any help here?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900