Since every form is a class it's not directly possible to access one form
from another. The solution is to make a public static property in the form
(class) you want to access. This property contains a reference to the
instance of that form. An example of the property is shown below, the
example is using a form called Form1 which is the main form for the
application:
//The property, added to Form1
private static Form1 m_mainform;
public static Form1 MAINFORM
{
get { return m_mainform; }
set { m_mainform = value; }
}
Example for using this property to store the main form (form1) in:
//Standard main procedure of Form1
static void Main()
{
Form1 frm = new Form1 (); //Create a new instance of the form Form1
Form1.MAINFORM = frm; //Store this instance in the public static
property
Application.Run(frm); //Show the form
}
Now you can use the following line of code in every other form:
Form1.MAINFORM.Label1.text = "testing";
Don't forget to set the control you want to access to public instead of the
default private.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.