Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing Variable to Parent form

Passing Variable to Parent form

Scheduled Pinned Locked Moved C#
question
27 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Christian Graus

    There's a great article on this here on CP. Short answer - a delegate.

    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

    B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #6

    i seen the article on delegate here http://www.codeproject.com/KB/cs/event_fundamentals.aspx#3.Delegates2[^ But i dont quite understand how it works. can u give me a simple example?

    C 1 Reply Last reply
    0
    • B benjamin yap

      Hi, i have a login form as a child form. I want to pass the value txtUsername to the parent form. How do i do it?

      N Offline
      N Offline
      natsuyaki
      wrote on last edited by
      #7

      Why don't you the login form as a dialog?

      B 1 Reply Last reply
      0
      • N natsuyaki

        Why don't you the login form as a dialog?

        B Offline
        B Offline
        benjamin yap
        wrote on last edited by
        #8

        i prefer a form with textbox nicer and more professional

        N 1 Reply Last reply
        0
        • N natsuyaki

          I admit that it's ugly. But it works.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #9

          Yes, but some of us write professional code, not just whatever works ( and your solution still does not work )

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          1 Reply Last reply
          0
          • B benjamin yap

            i prefer a form with textbox nicer and more professional

            N Offline
            N Offline
            natsuyaki
            wrote on last edited by
            #10

            Forms can be shown as dialog and return a DialogResult, that makes things simple.

            C 1 Reply Last reply
            0
            • B benjamin yap

              i seen the article on delegate here http://www.codeproject.com/KB/cs/event_fundamentals.aspx#3.Delegates2[^ But i dont quite understand how it works. can u give me a simple example?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #11

              If the child form contains this code public delegate void SendString(string s); public SendString OnSendString = null; Then your main form can do this public void GetString(string theString) { } and in your code that creates the child form: MyChild dlg = new MyChild(); dlg.OnSendString += new MyChild.SendString(this.GetString); Then when the child form does this: if (OnSendString != null) OnSendString("test"); the value "test" is called in GetString in the parent form. All of this assumes your child form is modeless. If it's modal, then you may as well just assign properties like the other guy said. I assumed that it was modeless from your initial question, you said you want to send something back to the main form. But, if the child form has been closed, then you're not sending anything, properties make more sense in that case. I can't find the article on communication between forms :(

              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              B 1 Reply Last reply
              0
              • N natsuyaki

                Forms can be shown as dialog and return a DialogResult, that makes things simple.

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #12

                Yes, if the form is modal, then it makes sense to do what you suggested, I just assumed from the question wording that it was modeless. In that case, I apologise, I would use properties and the DialogResult to return a value from a modal form.

                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                1 Reply Last reply
                0
                • C Christian Graus

                  If the child form contains this code public delegate void SendString(string s); public SendString OnSendString = null; Then your main form can do this public void GetString(string theString) { } and in your code that creates the child form: MyChild dlg = new MyChild(); dlg.OnSendString += new MyChild.SendString(this.GetString); Then when the child form does this: if (OnSendString != null) OnSendString("test"); the value "test" is called in GetString in the parent form. All of this assumes your child form is modeless. If it's modal, then you may as well just assign properties like the other guy said. I assumed that it was modeless from your initial question, you said you want to send something back to the main form. But, if the child form has been closed, then you're not sending anything, properties make more sense in that case. I can't find the article on communication between forms :(

                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                  B Offline
                  B Offline
                  benjamin yap
                  wrote on last edited by
                  #13

                  How do i know if my file is modeless or modal? I did not close my child form. I use .Hide(). I dont know whether i should close the form or use Hide. probably u might wanna take a look at my file here is my project file http://opencube.com.sg/EBMS.zip

                  C 1 Reply Last reply
                  0
                  • B benjamin yap

                    How do i know if my file is modeless or modal? I did not close my child form. I use .Hide(). I dont know whether i should close the form or use Hide. probably u might wanna take a look at my file here is my project file http://opencube.com.sg/EBMS.zip

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #14

                    If you call ShowDialog, it's modal. IF you call Show, so the parent form stays active, it's modeless, and that's why you need a delegate. If your main form code stops when you show the child, your main form code knows when the otehr form ends it's life, so you can examine it's state. If you called Show, you don't know when the child form has something to tell you, a delegate lets the form tell you. If you call Hide, then you're calling Show, I guess. Otherwise, your modal form would freeze your application. A login form has to be modal, or there's no point. A properties form, would be modeless, you change properties and see the results in the main form. Just some examples.

                    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                    B 1 Reply Last reply
                    0
                    • C Christian Graus

                      If you call ShowDialog, it's modal. IF you call Show, so the parent form stays active, it's modeless, and that's why you need a delegate. If your main form code stops when you show the child, your main form code knows when the otehr form ends it's life, so you can examine it's state. If you called Show, you don't know when the child form has something to tell you, a delegate lets the form tell you. If you call Hide, then you're calling Show, I guess. Otherwise, your modal form would freeze your application. A login form has to be modal, or there's no point. A properties form, would be modeless, you change properties and see the results in the main form. Just some examples.

                      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                      B Offline
                      B Offline
                      benjamin yap
                      wrote on last edited by
                      #15

                      Oh, so inside my FrmMain_Load(), i tried set my childLogin to ShowDialog i get this error "Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog." private void FrmMain_Load(object sender, EventArgs e) { this.toolStripStaff.Visible = false; this.toolStripCatalogue.Visible = false; this.toolStripProduct.Visible = false; this.toolStripOrder.Visible = false; this.toolStripInventory.Visible = false; FrmLogin childLogin = new FrmLogin(); childLogin.MdiParent = this; childLogin.ShowDialog(); stripLblLoginAs.Text = "Login As :"; }

                      C N 2 Replies Last reply
                      0
                      • B benjamin yap

                        Oh, so inside my FrmMain_Load(), i tried set my childLogin to ShowDialog i get this error "Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog." private void FrmMain_Load(object sender, EventArgs e) { this.toolStripStaff.Visible = false; this.toolStripCatalogue.Visible = false; this.toolStripProduct.Visible = false; this.toolStripOrder.Visible = false; this.toolStripInventory.Visible = false; FrmLogin childLogin = new FrmLogin(); childLogin.MdiParent = this; childLogin.ShowDialog(); stripLblLoginAs.Text = "Login As :"; }

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #16

                        benjamin yap wrote:

                        childLogin.MdiParent = this;

                        This is why you can't do it. Get rid of this line, it makes no sense to have it. Set te Owner property instead ( or Parent, I forget which, but I think it's Owner you want ). This stops your modal form from ever being hidden by it's parent.

                        benjamin yap wrote:

                        stripLblLoginAs.Text = "Login As :";

                        This is going to occur *after* your login form has shown. Now the easy easy way to make this work is to set the DIalogResult to DialogResult.OK only if the login succeeds. Then you can do if (childLogin.ShowDialog() == DialogResult.OK) { // logged in } else { // failed, close the program or whatever }

                        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                        B 1 Reply Last reply
                        0
                        • B benjamin yap

                          Oh, so inside my FrmMain_Load(), i tried set my childLogin to ShowDialog i get this error "Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog." private void FrmMain_Load(object sender, EventArgs e) { this.toolStripStaff.Visible = false; this.toolStripCatalogue.Visible = false; this.toolStripProduct.Visible = false; this.toolStripOrder.Visible = false; this.toolStripInventory.Visible = false; FrmLogin childLogin = new FrmLogin(); childLogin.MdiParent = this; childLogin.ShowDialog(); stripLblLoginAs.Text = "Login As :"; }

                          N Offline
                          N Offline
                          natsuyaki
                          wrote on last edited by
                          #17

                          childForm.ShowDialog(parentForm)

                          C 1 Reply Last reply
                          0
                          • N natsuyaki

                            childForm.ShowDialog(parentForm)

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #18

                            Yes, you can pass it on the constructor quite often, instead of setting it first. I know you can for a messagebox, for example.

                            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                            1 Reply Last reply
                            0
                            • C Christian Graus

                              benjamin yap wrote:

                              childLogin.MdiParent = this;

                              This is why you can't do it. Get rid of this line, it makes no sense to have it. Set te Owner property instead ( or Parent, I forget which, but I think it's Owner you want ). This stops your modal form from ever being hidden by it's parent.

                              benjamin yap wrote:

                              stripLblLoginAs.Text = "Login As :";

                              This is going to occur *after* your login form has shown. Now the easy easy way to make this work is to set the DIalogResult to DialogResult.OK only if the login succeeds. Then you can do if (childLogin.ShowDialog() == DialogResult.OK) { // logged in } else { // failed, close the program or whatever }

                              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                              B Offline
                              B Offline
                              benjamin yap
                              wrote on last edited by
                              #19

                              Christian, i am very sorry for asking so many question, I just started c# few days ago. Please bear with me childLogin.MdiParent = this; --> i though this is to link to the parent form? How do i set to owner property?

                              C N 2 Replies Last reply
                              0
                              • N natsuyaki

                                I admit that it's ugly. But it works.

                                C Offline
                                C Offline
                                Colin Angus Mackay
                                wrote on last edited by
                                #20

                                It also breaches one of the tenets of Object Oriented programming.

                                Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) My website | Blog

                                1 Reply Last reply
                                0
                                • B benjamin yap

                                  Christian, i am very sorry for asking so many question, I just started c# few days ago. Please bear with me childLogin.MdiParent = this; --> i though this is to link to the parent form? How do i set to owner property?

                                  C Offline
                                  C Offline
                                  Christian Graus
                                  wrote on last edited by
                                  #21

                                  This is a link in an MDI form, I've never used it and it apparently only works to create child forms within an MDI app. childLogin.Owner = this; will do what you want.

                                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                                  1 Reply Last reply
                                  0
                                  • B benjamin yap

                                    Christian, i am very sorry for asking so many question, I just started c# few days ago. Please bear with me childLogin.MdiParent = this; --> i though this is to link to the parent form? How do i set to owner property?

                                    N Offline
                                    N Offline
                                    natsuyaki
                                    wrote on last edited by
                                    #22

                                    A dialog is not a MdiChild, so childLogin.MdiParent = this; can be ignored. According to my experience: childForm child=new childForm(); child.ShowDialog(parent); works. My code and I is not professional. take careful advantage.

                                    B 1 Reply Last reply
                                    0
                                    • N natsuyaki

                                      A dialog is not a MdiChild, so childLogin.MdiParent = this; can be ignored. According to my experience: childForm child=new childForm(); child.ShowDialog(parent); works. My code and I is not professional. take careful advantage.

                                      B Offline
                                      B Offline
                                      benjamin yap
                                      wrote on last edited by
                                      #23

                                      hmm after i remove that line, and set to owner property, i get this error when i press the login button else if ((txtUsername.Text == username) && (txtPassword.Text == password)) { FrmMain frmMain = (FrmMain)this.MdiParent; frmMain.toolStripStaff.Visible = true; <----ERROR HERE this.Hide(); } Object reference not set to an instance of an object.

                                      N 1 Reply Last reply
                                      0
                                      • B benjamin yap

                                        hmm after i remove that line, and set to owner property, i get this error when i press the login button else if ((txtUsername.Text == username) && (txtPassword.Text == password)) { FrmMain frmMain = (FrmMain)this.MdiParent; frmMain.toolStripStaff.Visible = true; <----ERROR HERE this.Hide(); } Object reference not set to an instance of an object.

                                        N Offline
                                        N Offline
                                        natsuyaki
                                        wrote on last edited by
                                        #24

                                        FrmMain frmMain = (FrmMain)this.MdiParent your login form doesn't have a parent, so the frmMain will be null when running. try this: in main form: LoginForm login=new LoginForm(); login.ShowDialog(this); if (login.DialogResult==DialogResult.OK) { this.toolStripStaff.Visible = true; ........ in login form: else if ((txtUsername.Text == username) && (txtPassword.Text == password)) { this.DialogResult=DialogResult.OK; ........

                                        B 1 Reply Last reply
                                        0
                                        • B benjamin yap

                                          Hi, i have a login form as a child form. I want to pass the value txtUsername to the parent form. How do i do it?

                                          M Offline
                                          M Offline
                                          MoustafaS
                                          wrote on last edited by
                                          #25

                                          You can in your child form's constructor set a field to the opener of it, then at anytime call any method on that form using its reference called. ex:

                                          class Parent
                                          {
                                          public void Anymethod(){}
                                          public Parent()
                                          {
                                          Child c = new Child(this);
                                          }
                                          }
                                          class Child
                                          {
                                          private Form Owner;
                                          public Child(Form f)
                                          {Owner = f;}
                                          public void CallParent()
                                          {
                                          Owner.AnyMEthod();
                                          }
                                          }

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups