Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying data pass and call back from form2 textbox to form1 and form1 to form2 textboxes.
this below code is working fine but when i changed to f2.Show(); data could not transferd.
but f2.ShowDialog() is working perfectly.

many thanks.

What I have tried:

form1 code
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();

        //Step 1) 
        //Display the form passing parameter(s) via overloading 
        //the ShowDialog() method. 
        //In this example the parameter is the 'txtBoxForm1' on Form1.
        // f2.ShowDialog(); is replaced by
        f2.ShowDialog(ref txtBoxForm1); 

    }
}



form2
ublic partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    private void btnReturn_Click(object sender, EventArgs e)
    {
        this.Close(); 
    }

    //Step 2)
    //Receiving and returning parameter(s) via the overloaded ShowDialog() method.
    //This saves the need to have Properties and or fields associated
    //to parameters when overloading the above Form() constructor instead.
    public void ShowDialog(ref TextBox txtBoxForm1)
    {
        //Assign received parameter(s) to local context
        txtBoxForm2.Text = txtBoxForm1.Text;

        this.ShowDialog(); //Display and activate this form (Form2)

        //Return parameter(s)
        txtBoxForm1.Text = txtBoxForm2.Text;
    }
}
Posted
Updated 11-Apr-20 10:30am

1 solution

Please don't do that! Use Events instead - that's how the rest of .NET does this kind of thing, after all. Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

But ... do remember that ShowDialog creates a Modal form - which means that the thread calling ShowDialog does not return from the call until the new form has been closed - so absolutely nothing you try top do to it will occur until that happens. That's by design, and intended for things like Open and Save dialogs - use the Show method for everything else.
 
Share this answer
 
Comments
Member 10570811 12-Apr-20 2:49am    
many thanks,iam trying also mdi parent to child.but i could not solve this problem.
i want to try pass data from form2 to form1 whether i want to use Show(); in place of ShowDialog();

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