-1

All the tutorial in passing forms in the web are all from form1 then open form2 and then pass the value.

I want something reverse. form1 and form 2 is open so if you click ok in form2 the value you get in form 2 will be passed in form 1.

ex. form1 click buttton openform2button (form 2 shows) write 7 (number 7) on a textBox (in form2) click button okbutton then form 2 closes after form2 closes the textbox in form1 will store the data in form2. so the 7 you will put in textbox form2 will be saved and transferred in textbox form1. is that possible?

gdoron
  • 147,333
  • 58
  • 291
  • 367
rj tubera
  • 747
  • 4
  • 29
  • 51

2 Answers2

1

There's a number of ways you could do it:

  • Share a model object between form1 and form2.
  • Have form2 expose an event that form1 subscribes to. Pass the values in the event args.
  • Have form2 expose the value as a public property that form1 reads after form2 closes.
Andrew Kennan
  • 13,947
  • 3
  • 24
  • 33
  • Good answer. The option about sharing a common model is the most generic. It can be implemented in a number of ways. If we reduce the model to one value and keep this model in a public property of one of the forms we arrive at option three. Option two will work too, but logistics of subscribing/un-subscribing may become unneeded overhead. – Andrew Savinykh Jan 31 '12 at 01:46
  • is there an easy way to do it? – rj tubera Jan 31 '12 at 02:23
  • Exposing a property is probably the cleanest way. If you really want to, I'm sure you can have a class with a static property that can be set by the forms, but that code is not as reuseable. – Jason Dam Jan 31 '12 at 02:55
0

You can just have a public property on Form2 that Form1 will access when it needs to see the value. Something like this:

using(Form2 form2 = new Form2()) 
{
  if(form2.ShowDialog() == DialogResult.OK) 
  {
    form1Logger(form2.NumberWritten);
  }
}

The property can be something as simple as this:

class Form2
{
     public String NumberWritten 
     {
         get{return textBox.Value;}
     }
}
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180