Passing value from one form to another OPENED form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustRun
    New Member
    • Mar 2008
    • 127

    Passing value from one form to another OPENED form

    Hi,

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

    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
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

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





    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!

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Good memory Gary. I'm impressed.

      Comment

      • guimel
        New Member
        • Oct 2009
        • 7

        #4
        Originally posted by GaryTexmo
        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:
        Code:
        void Configure(){
        frmConfigure frmconf = new frmConfigure(Title, setting1, setting2);
        if (frmconf.ShowDialog() == DialogResult.OK){
          setting1 = frmconf.Setting1;
          setting2 = frmconf.Setting2;
        }
        where
        Code:
        partial class frmConfigure{
        public string Setting1 { get {return TextBox1.Text; } }
        public string Setting2 { get {return TextBox2.Text; } }
        }
        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 ?

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          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 :)

          Comment

          • JustRun
            New Member
            • Mar 2008
            • 127

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

            Comment

            Working...