Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wnat to pass back some parameters from a pop-up-form to the main/parent form and I am not using the MDI form.
For example:
A user clicks a button in form1 and it creats form2 by ShowDialog() but form1 will not close.
And then he types something in the textbox in form2.
I want the words in the textbox in form2 pass back to form1 when form2 is closed or when the user click the OK button in form2.
Also, what the user had typed in the textbox in form2 can immediately show in the ladel in form1.
Will it be difficult?

When I search in the Internet, a lot of examples are passing the data to the other form when that form is being created.
So, could anyone help me?
Posted

No, it's not difficult.
The easiest way to do it (and the recommended if you are using ShowDialog) is to set up a Property in Form2 whichs gets and sets the value of your TextBox:

Form2:
C#
public string UserName
   {
   get { return myUserNameTextBox.Text; }
   set { myUserNameTextBox.Text = value; }
   }


Form1:
C#
Form2 f = new Form2();
f.UserName = "Default Name";
f.ShowDialog();
MessageBox.Show(f.UserName);
This way, the internals of Form2 are hidden - you could use first name and last name text boxes and Form1 would never need to konow - it just gets the whole name via the UserName property.
 
Share this answer
 
After you show the form, it still exists until it is no longer referenced, so you can access any property of Form 2 from Form 1

e.g.


Form2 f2 = new Form2();
f2.ShowDialog();
Self.TextBox1.Text = f2.SomeProperty;


So you just need to set up a property on your Form2, something like

Public String SomeProperty
{
get
{
    return TextBox6.Text;
}
}
 
Share this answer
 

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