3

Consider two forms: form1 and form2

form1 calls form2:

form2 frm = new form2()
frm.show

form2 shows a grid with data. When data is selected, it's put into a textbox.

I need the return value from form2 to form1 when it's closed.

How can this be done?

user2864740
  • 60,010
  • 15
  • 145
  • 220
user459057
  • 53
  • 1
  • 2
  • 5

2 Answers2

5

Add a public property to your Form2 class that returns the selected item.

Then, replace the Show() call with ShowDialog() (a blocking method) and check the property afterwards.

Also, rename your forms.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

if you change Modifiers Property of a control in a Form to Public, another Forms can access to that control. f.e. :

Form2 frm; 
private void Form1_Load(object sender, EventArgs e) 
{ 
    frm = new Form2(); 
    frm.Show(); 
} 
private void button1_Click(object sender, EventArgs e) 
{ 
    MessageBox.Show(frm.txtUserName.Text); 
    //txtUserName is a TextBox with Modifiers=Public
}