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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Passing data from a form???

Passing data from a form???

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 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.
  • R Offline
    R Offline
    richiemac
    wrote on last edited by
    #1

    Hi guys, I've got a form that has a number of textboxes. When a button is clicked I want the data in the boxes to be sent to the class that opened the form. How do I go about doing this? In myClass I call the form using myForm.Show (); This opens the form but how then do I get the data from it? It seems to want an instance of the calling class in the myForm, but I can't do this.:confused: Hope that makes sense.

    L R 2 Replies Last reply
    0
    • R richiemac

      Hi guys, I've got a form that has a number of textboxes. When a button is clicked I want the data in the boxes to be sent to the class that opened the form. How do I go about doing this? In myClass I call the form using myForm.Show (); This opens the form but how then do I get the data from it? It seems to want an instance of the calling class in the myForm, but I can't do this.:confused: Hope that makes sense.

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      You could expose some properties that the calling class will get when the form is closed. This may be done in numerous ways, one would be to catch the Form.Closed event (the calling class catches the one raised by the called form).private void OnMyFormClosed(object sender, EventArgs cea) { MyForm myForm = (MyForm)sender; string textBox1content = myForm.TextBox1Content; //... You get the idea. }

      ------------------ As I suggested, there are numerous other ways, like opening the form in modal mode and just wait for the ShowDialog() method to return, but still using properties. Also, you could use a custom event with a EventArgs-derived class containing the data of interest. -------- "I say no to drugs, but they don't listen." - Marilyn Manson -- modified at 12:40 Tuesday 28th February, 2006

      R L 2 Replies Last reply
      0
      • L Le centriste

        You could expose some properties that the calling class will get when the form is closed. This may be done in numerous ways, one would be to catch the Form.Closed event (the calling class catches the one raised by the called form).private void OnMyFormClosed(object sender, EventArgs cea) { MyForm myForm = (MyForm)sender; string textBox1content = myForm.TextBox1Content; //... You get the idea. }

        ------------------ As I suggested, there are numerous other ways, like opening the form in modal mode and just wait for the ShowDialog() method to return, but still using properties. Also, you could use a custom event with a EventArgs-derived class containing the data of interest. -------- "I say no to drugs, but they don't listen." - Marilyn Manson -- modified at 12:40 Tuesday 28th February, 2006

        R Offline
        R Offline
        richiemac
        wrote on last edited by
        #3

        I've used the ShowDialog method with properties and it works fine. Knew it would be fairly straight forward - just not for me. :rolleyes:

        L 1 Reply Last reply
        0
        • R richiemac

          I've used the ShowDialog method with properties and it works fine. Knew it would be fairly straight forward - just not for me. :rolleyes:

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          :-> -------- "I say no to drugs, but they don't listen." - Marilyn Manson

          1 Reply Last reply
          0
          • R richiemac

            Hi guys, I've got a form that has a number of textboxes. When a button is clicked I want the data in the boxes to be sent to the class that opened the form. How do I go about doing this? In myClass I call the form using myForm.Show (); This opens the form but how then do I get the data from it? It seems to want an instance of the calling class in the myForm, but I can't do this.:confused: Hope that makes sense.

            R Offline
            R Offline
            Robin Panther
            wrote on last edited by
            #5

            If you want to send some data to another object - you have to have reference to this object. In your case this means that your myForm should have reference to base form. something like this: call the form using myForm.Show(this) or set myForm.Owner = this just befor calling. Then, on button clicks, you could do something like this: ((OwnerForm)Owner).OkButtonIsClicked(); You could get values of textboxes of myForm if their modifiers are internal or public, but yYou could do it only if OwnerForm has reference to myForm, if not - you can invoke not OkButonIsClicked(), but OkButtonIsClicked(textBox1.Text, textBox2.Text, etc); ____________________________________________ Robin Panther http://www.robinland.com

            L 1 Reply Last reply
            0
            • R Robin Panther

              If you want to send some data to another object - you have to have reference to this object. In your case this means that your myForm should have reference to base form. something like this: call the form using myForm.Show(this) or set myForm.Owner = this just befor calling. Then, on button clicks, you could do something like this: ((OwnerForm)Owner).OkButtonIsClicked(); You could get values of textboxes of myForm if their modifiers are internal or public, but yYou could do it only if OwnerForm has reference to myForm, if not - you can invoke not OkButonIsClicked(), but OkButtonIsClicked(textBox1.Text, textBox2.Text, etc); ____________________________________________ Robin Panther http://www.robinland.com

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #6

              I would not recommend this approach. This design is known as "tight coupling" or "re-coupling", and is an anti-pattern[^]. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

              1 Reply Last reply
              0
              • L Le centriste

                You could expose some properties that the calling class will get when the form is closed. This may be done in numerous ways, one would be to catch the Form.Closed event (the calling class catches the one raised by the called form).private void OnMyFormClosed(object sender, EventArgs cea) { MyForm myForm = (MyForm)sender; string textBox1content = myForm.TextBox1Content; //... You get the idea. }

                ------------------ As I suggested, there are numerous other ways, like opening the form in modal mode and just wait for the ShowDialog() method to return, but still using properties. Also, you could use a custom event with a EventArgs-derived class containing the data of interest. -------- "I say no to drugs, but they don't listen." - Marilyn Manson -- modified at 12:40 Tuesday 28th February, 2006

                L Offline
                L Offline
                Lyle M
                wrote on last edited by
                #7

                Could you expound on your explaination a little. Is the OnMyFormClosed() function located in the parent form? How do you set an event to trigger when the child closes? Thanks Lyle

                L 1 Reply Last reply
                0
                • L Lyle M

                  Could you expound on your explaination a little. Is the OnMyFormClosed() function located in the parent form? How do you set an event to trigger when the child closes? Thanks Lyle

                  L Offline
                  L Offline
                  Le centriste
                  wrote on last edited by
                  #8

                  Lyle M wrote:

                  Is the OnMyFormClosed() function located in the parent form?

                  Yes

                  Lyle M wrote:

                  How do you set an event to trigger when the child closes?

                  The event is automatically triggered when MyForm closes. To subscribe to it, the parent form needs to do the following, before calling Form.Show(). myForm.Closed += new EventHandler(this.OnMyFormClosed); -------- "I say no to drugs, but they don't listen." - Marilyn Manson

                  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