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.
  • 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
            • N natsuyaki

              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 Offline
              B Offline
              benjamin yap
              wrote on last edited by
              #26

              Great! Thanks alot! So how do i pass the value of the txtUsername inside my Login to my statusstrip at my mainform stripLblLoginAs.Text = "Login As :" + login.txtUsername.Text;

              N 1 Reply Last reply
              0
              • B benjamin yap

                Great! Thanks alot! So how do i pass the value of the txtUsername inside my Login to my statusstrip at my mainform stripLblLoginAs.Text = "Login As :" + login.txtUsername.Text;

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

                Err, I see, txtUsername is private huh? define a property: public string Username { get{return txtUsername.Text;} } So, in main form: stripLblLoginAs.Text = "Login As :" + login.Username;

                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