Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, How can I accesss the controls of one form from another form?
How can I access it's methods and private fields?
Posted
Updated 25-Feb-12 2:35am
v2
Comments
Shahin Khorshidnia 25-Feb-12 9:47am    
No way!
If you have a encapsulated form with some private fields. You can not directly access to it's private fields from another form.

There are several things you can do. First, you could pass the controls to your other form, using a constructor or property. Another method (the preferred one) is passing your Form to the other Form as an Interface, which has some methods like "Update". When you want the Form to update then simply call this (check the option of passing values to constructors below). You should never have direct access to private fields. Always encapsulate them through methods and/or properties. I have pasted this answer from a question I have answered earlier. They are only a few options to pass objects between forms. The one using a Method could also apply here.

To pass Objects between Forms you can follow one of these approaches:
One is using a Property on one of your Forms.
C#
MyForm frm = new MyForm();
// It is possible to set MyValue at any point in your code, as long as you have a reference to an instance of MyForm.
frm.MyValue = "some value";
frm.Show();

Another is passing it to the constructor.
C#
public partial class MyForm : Form
{
   private IMyInterface _aForm;
   // Use an Interface an Implements it in the Form you want to update.
   // Pass that Form to the constructor.
   public MyForm(IMyInterface aForm)
   {
      InitializeComponent();
      _aForm = Form;
      // Possibly use _aForm here.
   }
   // Do stuff with _aForm here.
}
Usage would look like this:
C#
// Make sure 'this' actually implements the Interface.
MyForm frm = new MyForm(this);
frm.Show();

Another approach could be to use a Method...
C#
public partial class MyForm : Form
{
   private String _myValue;
   public void SetValue(String myValue)
   {
      _myValue = myValue;
     // Possibly do stuff with myValue here.
   }
   // Or use _myValue here.
}
Usage:
C#
MyForm frm = new MyForm();
frm.Show();
// Once again, you can use this anywhere in your code, as long as you have a reference to the instance of MyForm.
frm.SetValue("some value");
So now you got your data in another Form, all you need to do is handle it there, edit it etc. and update it in the Form it originally came from.

Hope it helps :)
 
Share this answer
 
You can't access form1 controls from form2, since they are private to the form1.
If you want to set Form1 control value based on Form2's some action then you should create Event in Form2 and Form1 can subscribe to the event. When any change happen on Form2 fire the event and pass the information as eventargs which form1 can access.

Please note: you can never access form1 control or private fields directly from Form2 since they are private.
 
Share this answer
 
Comments
Sander Rossel 25-Feb-12 14:52pm    
Actually, the default access modifier for Controls is Friend/internal (at least in VB). In C# it's private, but you can set it to anything you like (it's the Modifiers Property in the Property Grid).
I never understood why Microsoft chose to make Controls in VB Friend (probably some compatibility issues with VB6 or earlier...). It does bother me a bit, since I work in VB in my every day job. I think private is the way to go for Controls.
An Event is also a nice alternative here, so have my 5!
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

I must add that the best UI design uses just one form; and everything which needs to be changed or navigated happens inside that form: docking, tabbing interfaces, etc.

—SA
 
Share this answer
 
This is never a good idea. By doing this, you bind one form to another making each unusable by itself.

The proper way to do it would be for one form to package up its data into a class or structure and another form can pickup that data object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900