Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing value from one form to another OPENED form

JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 110
#1: Oct 20 '09
Hi,

I have two form, MainForm has link do the following:
frmChild.showDialog();

the frmChild process some data then should return value to MainForm at TxtBox control

How can i do this?

I need a quick reply plzzz
Thanks

Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 228
#2: Oct 20 '09

re: Passing value from one form to another OPENED form


Thlintoq has been involved in a few discussions on this very topic around here, have a look and see if they help you out :)

How do I carry out a command for a control on one form to another

Accessing Components of One Form From Another

Also, since the form is being shown as a dialog, you can always expose some public properties that your main form can read when you're finished. Just remember that you can't directly read the value of a control because, as the form has closed, those controls are now disposed. You'd have to copy the value of the textbox to a private member when the form closes (or in some other circumstance of your choosing).

Good luck!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#3: Oct 20 '09

re: Passing value from one form to another OPENED form


Good memory Gary. I'm impressed.
Newbie
 
Join Date: Oct 2009
Posts: 7
#4: Oct 22 '09

re: Passing value from one form to another OPENED form


Quote:

Originally Posted by GaryTexmo View Post

Also, since the form is being shown as a dialog, you can always expose some public properties that your main form can read when you're finished. Just remember that you can't directly read the value of a control because, as the form has closed, those controls are now disposed.

Is this true ? (that you can't access the controls once ShowDialog has returned). I often have the following:
Expand|Select|Wrap|Line Numbers
  1. void Configure(){
  2. frmConfigure frmconf = new frmConfigure(Title, setting1, setting2);
  3. if (frmconf.ShowDialog() == DialogResult.OK){
  4.   setting1 = frmconf.Setting1;
  5.   setting2 = frmconf.Setting2;
  6. }
  7.  
where
Expand|Select|Wrap|Line Numbers
  1. partial class frmConfigure{
  2. public string Setting1 { get {return TextBox1.Text; } }
  3. public string Setting2 { get {return TextBox2.Text; } }
  4. }
  5.  
My reading of the designer generated code for frmconf is that the components are only disposed when the form is disposed. Now, since I still have a reference to frmconf after ShowDialog returns, the form should not be disposed and the controls still exists.
Is this correct, or have I just been lucky that my program hasn't crashed so far ?
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 228
#5: Oct 22 '09

re: Passing value from one form to another OPENED form


You're absolutely correct... I had a problem a while back with a modal dialog disposing on me when I didn't want to and my brain must have associated it with that, but I just did a quick test and that code is fine. The IsDisposed property is also false, doubly confirming that you're correct.

Looking at that code, I see where I got mixed up. If you're interested in the details, throw me a PM, but I don't think it belongs in this thread (I don't want to confuse things further!). About all I'll say is that a form called with ShowDialog() will return and the reference will stay active, but a form called with Show() will return and dispose.

Either way, you're correct, you don't need to worry about your form disposing and your code is just fine :)
JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 110
#6: 3 Weeks Ago

re: Passing value from one form to another OPENED form


Sorry for the late reply, I really was so sick.
Thanks Guimel and Gary, you helped a lot :)
Reply