I am creating an application where a user will click a button on form1, which will cause form2 to display. The user will then fill out a chat on form2 and then click a button that will close form2 and send back parameters to form1 for processing. How can I do this in C#? I have seen people using properties to do this, but the examples are not clear enough. Can someone give some example code showing me how I can pass the parameters? I would prefer the properties method, but as long as it works, I will count it as the answer.
2 Answers
Put simply, place your form elements in the second form as you typically would. Then, you can add public accessors to that form that you can then pull from and reference. For instance, if Form2 has a text fields you wanted to pull back, you could:
class Form2
{
// Form2.designer.cs
private TextBox TextBox1;
// Form2.cs
public String TextBoxValue // retrieving a value from
{
get
{
return this.TextBox1.Text;
}
}
public Form2(String InitialTextBoxValue) // passing value to
{
IntiializeComponent();
this.TextBox1.Text = InitialTextBoxValue;
}
}
Then just access it later when you create the form (much like the OpenFileDialog does for Filename etc.
public void Button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2("Bob"); // Start with "Bob"
form2.ShowDialog(); // Dialog opens and user enters "John" and closes it
MessageBox.Show(form2.TextBoxValue); // now the value is "John"
}
Same thing can be done for Int32, Boolean, etc. Just depends on the form's value, if you'd like to cast/validate it, or otherwise.
Alternativly, you can play with the Modifiers
property within the form designer where you can make the control public so it's accessible externally. I personally recommend using an accessor so you can validate and confirm the returned results rather than just dumping the value (as this logic is typically found in the form itself, not in every instance you want to call/use Form2)

- 100,477
- 16
- 156
- 200
-
Thanks, also is there any way I would be able to pass form2 parameters from form1? The same code would create a second form1, right? – Franz Payer Jan 15 '11 at 19:45
-
You can take advantance of the construct in Form2 to supply parameters, or you can use other properties you'd set before your call to ShowDialog (but you'd need to make sure to make any modifications to controls after `InitializeComponent()` if those proeprties are actually affecting the way the form's displayed). – Brad Christie Jan 15 '11 at 19:48
-
@DazSlayer: I've added more information to the sample to show pushing values to and pulling values from another form. – Brad Christie Jan 15 '11 at 20:01
Here is how I like to pass parameter(s) to and back from another form:
Provided the following form design for Form1 and Form2:
In this example, the 'txtBoxForm1' textbox on Form1 is passed to Form2 where it is used to initialize the 'txtBoxForm2' textbox on Form2. After the user interacts with Form2 and ends it by clicking on the [Return to Form1] button, the value of the 'txtBoxForm2' textbox is assigned to the parameter that is returned (by reference) to the 'txtBoxForm1' textbox on Form1.
Coding this is done in two simple steps:
Step 1) In Form1, pass parameter(s) to Form2 by overloading the ShowDialog() method:
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);
}
}
In the above code, the parameter is the 'txtBoxForm1' textbox itself that is passed by reference. Passing it by reference is why it not only passes the textbox value to Form2, but it can also receive and display on Form1 any modification applied to that textbox parameter while executing Form2.
I put the whole Form1 class to show that nothing else is special in this class than the overloading of the 'f2.ShowDialog()' method call.
Step 2) Receiving and returning parameter(s) via the overloaded ShowDialog() method:
public 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;
}
}
Again I supplied the complete form class (Form2 in this case) code to show how limited is the coding intervention. No additional field or property are required here. That is because I used the 'ShowDialog()' instruction here rather than the Form2() constructor from Form1 to pass the parameter. Contrary to the Form2() constructor, The ShowDialog() method is an envelope around the user interaction phase with Form2. As such, its timing allows the '(ref txtBoxForm1)' parameter to be the complete and sufficient representative of the parameter we wish to send and receive.
Overloading a method by re-declaring it with a different set of parameters (also called signature), is a powerful feature of C# .net. In this case here, it allows on one hand to add parameter(s) to the call of the 'ShowDialog()' method and on the other hand the overloaded method loses nothing from the original version of this same method because the original version of the ShowDialog() .net framework method is executed as well via the 'this.ShowDialog();' instruction.
That is it for me about this for now.
Hoping this helps!

- 261
- 3
- 5