How can I effectively access the object collection of form1 to another form in WinApp Project?
-
I having difficulty accessing the object from form1 to another form in WinApp Project, unlike in Visual Studio 6 that you can access directly the object by Form1.Object1.text. How can I do this either in VB.NET or C# in WinApp Project? Thanks
-
I having difficulty accessing the object from form1 to another form in WinApp Project, unlike in Visual Studio 6 that you can access directly the object by Form1.Object1.text. How can I do this either in VB.NET or C# in WinApp Project? Thanks
Hi, You can access an object of one form in another by 3 ways... 1) Make the object public or internal 2) Keep the object private but expose a public property for the same. 3) Pass the object in the constructor of the second form and then in the second form declare a private variable, initialize it in the constructor and access the object. class form1 { private ArrayList array = new ArrayList(); .... ... button1_click(...) { form2 = new form2(array); form2.Show(); } } class form2 { private ArrayList objArray; form2(ArrayList obj) { objArray = obj; } button2_click(...) { MessageBox.Show(objArray.Count.ToString()); } } Hope this code helps you to understand the 3rd possibility... and also hope that I haven't misunderstood your question. regards, Aryadip. Cheers !! and have a Funky day !!
-
Hi, You can access an object of one form in another by 3 ways... 1) Make the object public or internal 2) Keep the object private but expose a public property for the same. 3) Pass the object in the constructor of the second form and then in the second form declare a private variable, initialize it in the constructor and access the object. class form1 { private ArrayList array = new ArrayList(); .... ... button1_click(...) { form2 = new form2(array); form2.Show(); } } class form2 { private ArrayList objArray; form2(ArrayList obj) { objArray = obj; } button2_click(...) { MessageBox.Show(objArray.Count.ToString()); } } Hope this code helps you to understand the 3rd possibility... and also hope that I haven't misunderstood your question. regards, Aryadip. Cheers !! and have a Funky day !!
Thanks for the response.... but i still don't get it.... This is the scenario, I have two forms one is a parent form and the second is the lookup form. I need to access one of the textbox in parent and retrieve the value given by the lookup vise versa. How can I access the specific textbox in parent and lookup form? Thanks again..
-
Thanks for the response.... but i still don't get it.... This is the scenario, I have two forms one is a parent form and the second is the lookup form. I need to access one of the textbox in parent and retrieve the value given by the lookup vise versa. How can I access the specific textbox in parent and lookup form? Thanks again..
hi, look what you can do in this senario is : On the button click event (before showing the 2nd form)... Store the value of the textbox in a public variable... Pass the object the the parent class to the 2nd form thru constructor In the 2nd form use the parent form's object to access the public variable of the parent. Now to access the textbox of the 2nd form from the parent: declare a public variable in the 2nd form and set itz value with the text box value... In the parent form you already have the object of the send... bcoz you r showing the 2nd from the parent... So use the object to access the public variable of the 2nd form... for example: // the parent form class parent : Form { // public variable in parent form to hold the textbox value public string textVal = ""; ... ... // button click event delegate in parent form private void button_click(...) { // store the textbox value in the public variable textVal = textBox1.Text; // instantiate child form with parent object in the constructor childForm form2 = new childForm(this); // showing the child form as modal dialog form2.ShowDialog(); // after dialog is closed accessing the textbox value of the child form MessageBox.Show(form2.textValChild); // destroying the child form form2.Close(); } } // the child form class class childForm : Form { // public variable in the child form to store the textbox value of the child public string textValChild = ""; private Form objparent = null; // child form constructor with the parent form object public childForm(Form parent) { this.objparent = parent; } ... ... // in the button click event delegate private void Textbox2_Click(...) { // displaying the parent form textbox value MessageBox.Show(this.objparent.textVal); // storing the child form textbox value this.textValChild = TextBox2.Text; //hiding the child form... don't close otherwise itz object will be destroyed this.Hide(); } } Hope this helps you... regards, Aryadip. Cheers !! and have a Funky day !!
-
hi, look what you can do in this senario is : On the button click event (before showing the 2nd form)... Store the value of the textbox in a public variable... Pass the object the the parent class to the 2nd form thru constructor In the 2nd form use the parent form's object to access the public variable of the parent. Now to access the textbox of the 2nd form from the parent: declare a public variable in the 2nd form and set itz value with the text box value... In the parent form you already have the object of the send... bcoz you r showing the 2nd from the parent... So use the object to access the public variable of the 2nd form... for example: // the parent form class parent : Form { // public variable in parent form to hold the textbox value public string textVal = ""; ... ... // button click event delegate in parent form private void button_click(...) { // store the textbox value in the public variable textVal = textBox1.Text; // instantiate child form with parent object in the constructor childForm form2 = new childForm(this); // showing the child form as modal dialog form2.ShowDialog(); // after dialog is closed accessing the textbox value of the child form MessageBox.Show(form2.textValChild); // destroying the child form form2.Close(); } } // the child form class class childForm : Form { // public variable in the child form to store the textbox value of the child public string textValChild = ""; private Form objparent = null; // child form constructor with the parent form object public childForm(Form parent) { this.objparent = parent; } ... ... // in the button click event delegate private void Textbox2_Click(...) { // displaying the parent form textbox value MessageBox.Show(this.objparent.textVal); // storing the child form textbox value this.textValChild = TextBox2.Text; //hiding the child form... don't close otherwise itz object will be destroyed this.Hide(); } } Hope this helps you... regards, Aryadip. Cheers !! and have a Funky day !!
hi, I already pass the value of parent form to the second form. This time the value passed by the parent form already displayed by the second form in textbox for editing. After editing the second form must pass this value to the parent form. Now I need to pass the value of the second form back to parent form without declaring another object of the parent form. Note : The Second form is in MODAL STATE or DIALOG STATE. Thanks And More Power!
-
hi, I already pass the value of parent form to the second form. This time the value passed by the parent form already displayed by the second form in textbox for editing. After editing the second form must pass this value to the parent form. Now I need to pass the value of the second form back to parent form without declaring another object of the parent form. Note : The Second form is in MODAL STATE or DIALOG STATE. Thanks And More Power!
hi, no need to declare another object of the parent form... look at my previous code carefully... I have set the value of the second form textbox in the click event of childForm and then instead of closing the second form I have hidden it...has come back to the parent form and then accessed the second form value from the parent... Please go thru the code carefully to understand what I'm saying... read the Click events carefully... regards, Aryadip. Cheers !! and have a Funky day !!
-
I having difficulty accessing the object from form1 to another form in WinApp Project, unlike in Visual Studio 6 that you can access directly the object by Form1.Object1.text. How can I do this either in VB.NET or C# in WinApp Project? Thanks
When creating the child Form in the Parent form, set the Child form's Owner property to be the parent. Then make some get property in the parent or a set property in the child to be get/set whenever the Object in the parent form chages (using events usually does the trick..)