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. How to make data from one form available to another

How to make data from one form available to another

Scheduled Pinned Locked Moved C#
csshelptutorial
11 Posts 5 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.
  • I Offline
    I Offline
    I explore code
    wrote on last edited by
    #1

    hi, I have two forms Form1 and Form2. On Form1 i have a data grid that has a data in 3 columns, now on button click i want to send data of first row to Form2 that has 3 textboxes, so that the text boxes have the corresponding values (for eg Name,City,Country). I tried to make 3 public properties and assign the values of the grid cells to them and then tried to call these properties on FOrm2 to populate the text boxes, but they show up empty, because to be able to access Form1's property, i need to declare and initialise Form1's instance, so each time a new instance is created, the property would contain null values.Please help

    R C G C 4 Replies Last reply
    0
    • I I explore code

      hi, I have two forms Form1 and Form2. On Form1 i have a data grid that has a data in 3 columns, now on button click i want to send data of first row to Form2 that has 3 textboxes, so that the text boxes have the corresponding values (for eg Name,City,Country). I tried to make 3 public properties and assign the values of the grid cells to them and then tried to call these properties on FOrm2 to populate the text boxes, but they show up empty, because to be able to access Form1's property, i need to declare and initialise Form1's instance, so each time a new instance is created, the property would contain null values.Please help

      R Offline
      R Offline
      rah_sin
      wrote on last edited by
      #2

      u can pass the instance of first form to second form in second forms constructor.

      rahul

      C I 2 Replies Last reply
      0
      • R rah_sin

        u can pass the instance of first form to second form in second forms constructor.

        rahul

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

        Except that would be nasty...

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        1 Reply Last reply
        0
        • I I explore code

          hi, I have two forms Form1 and Form2. On Form1 i have a data grid that has a data in 3 columns, now on button click i want to send data of first row to Form2 that has 3 textboxes, so that the text boxes have the corresponding values (for eg Name,City,Country). I tried to make 3 public properties and assign the values of the grid cells to them and then tried to call these properties on FOrm2 to populate the text boxes, but they show up empty, because to be able to access Form1's property, i need to declare and initialise Form1's instance, so each time a new instance is created, the property would contain null values.Please help

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

          I assume they are not called that.... If you want to pass data from form1 to form2, pass it through the constructor or through properties, if you want to pass data from the child to the parent, set up delegate methods to do so. Whatever you do, set up the smallest scope properties you can, i.e. set up a string property to set the text of a textbox, rather than making the textbox itself public.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          I 2 Replies Last reply
          0
          • R rah_sin

            u can pass the instance of first form to second form in second forms constructor.

            rahul

            I Offline
            I Offline
            I explore code
            wrote on last edited by
            #5

            public partial class Form2 : Form { Form1 frm1; public Form2() { InitializeComponent(); frm1 = new Form1(); } Is that what u wan to say ?

            C 1 Reply Last reply
            0
            • C Christian Graus

              I assume they are not called that.... If you want to pass data from form1 to form2, pass it through the constructor or through properties, if you want to pass data from the child to the parent, set up delegate methods to do so. Whatever you do, set up the smallest scope properties you can, i.e. set up a string property to set the text of a textbox, rather than making the textbox itself public.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              I Offline
              I Offline
              I explore code
              wrote on last edited by
              #6

              Hi Chris....thts what I precisely did....i created 3 public properties on Form1 each to capture cell values of the grid data and then in Form2 I created an instance of Form1 and using it i accessed those properties, but since each time the Form2 loads, it would create a new instance which wud be null, the text boxes are empty. Now, do we have a way to modify this to work

              C 1 Reply Last reply
              0
              • I I explore code

                hi, I have two forms Form1 and Form2. On Form1 i have a data grid that has a data in 3 columns, now on button click i want to send data of first row to Form2 that has 3 textboxes, so that the text boxes have the corresponding values (for eg Name,City,Country). I tried to make 3 public properties and assign the values of the grid cells to them and then tried to call these properties on FOrm2 to populate the text boxes, but they show up empty, because to be able to access Form1's property, i need to declare and initialise Form1's instance, so each time a new instance is created, the property would contain null values.Please help

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                This will help you http://www.codeproject.com/dotnet/passingvaluesbetweenforms.asp[^]

                #region signature my articles #endregion

                1 Reply Last reply
                0
                • C Christian Graus

                  I assume they are not called that.... If you want to pass data from form1 to form2, pass it through the constructor or through properties, if you want to pass data from the child to the parent, set up delegate methods to do so. Whatever you do, set up the smallest scope properties you can, i.e. set up a string property to set the text of a textbox, rather than making the textbox itself public.

                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  I Offline
                  I Offline
                  I explore code
                  wrote on last edited by
                  #8

                  Hey Chris and Rahul.....thannx a lot ..constructor technique worked .....but is there any other way to do it?

                  1 Reply Last reply
                  0
                  • I I explore code

                    public partial class Form2 : Form { Form1 frm1; public Form2() { InitializeComponent(); frm1 = new Form1(); } Is that what u wan to say ?

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

                    No, that is an utter disaster, it cannot work. The Form1 inside form2 is completely irrelevant to the Form1 that you are working with.

                    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    1 Reply Last reply
                    0
                    • I I explore code

                      Hi Chris....thts what I precisely did....i created 3 public properties on Form1 each to capture cell values of the grid data and then in Form2 I created an instance of Form1 and using it i accessed those properties, but since each time the Form2 loads, it would create a new instance which wud be null, the text boxes are empty. Now, do we have a way to modify this to work

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

                      Yep - that's the issue. A new instance of Form1 has nothing to do with the instance in use in your program. I recommend reading a book on OO.

                      gladiatron wrote:

                      Now, do we have a way to modify this to work

                      Going from form2 to form1, you should use delegates.

                      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      1 Reply Last reply
                      0
                      • I I explore code

                        hi, I have two forms Form1 and Form2. On Form1 i have a data grid that has a data in 3 columns, now on button click i want to send data of first row to Form2 that has 3 textboxes, so that the text boxes have the corresponding values (for eg Name,City,Country). I tried to make 3 public properties and assign the values of the grid cells to them and then tried to call these properties on FOrm2 to populate the text boxes, but they show up empty, because to be able to access Form1's property, i need to declare and initialise Form1's instance, so each time a new instance is created, the property would contain null values.Please help

                        C Offline
                        C Offline
                        Chintan Desai
                        wrote on last edited by
                        #11

                        obviously when you create any object instance, it allocate new empty memory. So just do one thing in form1 click event.create form2 instance, after then assign grid cell in form1 to form2 public property.Now in form2 load event assign that property to each textbox of form2.Now show form2.:rose:

                        Regards Chintan www.visharadsoft.com (Nothing is so purify as KNOWLEDGE)

                        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