How to make data from one form available to another
-
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
-
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
-
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 )
-
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
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 )
-
public partial class Form2 : Form { Form1 frm1; public Form2() { InitializeComponent(); frm1 = new Form1(); }
Is that what u wan to say ? -
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 )
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
-
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
This will help you http://www.codeproject.com/dotnet/passingvaluesbetweenforms.asp[^]
#region signature my articles #endregion
-
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 )
Hey Chris and Rahul.....thannx a lot ..constructor technique worked .....but is there any other way to do it?
-
public partial class Form2 : Form { Form1 frm1; public Form2() { InitializeComponent(); frm1 = new Form1(); }
Is that what u wan to say ?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 )
-
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
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 )
-
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
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)