You are referencing testButton1
like it was a static field instead of an instance field. You need to be able to access the instance of the form. You can do this like this:
public partial class Form1 : Form
{
public static Form1 Instance { get; private set; }
public Form1()
{
InitializeComponent();
if (Instance != null)
throw new Exception("Only one instance of Form1 is allowed");
Instance = this;
FormClosed += new FormClosedEventHandler(Form1_FormClosed);
}
void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Instance = null;
}
public Button TestButton1 { get { return testButton1; } }
}
Because controls on the form are protected by default, you have to make the button accessible. You do this using the TestButton1
property.
Now you can access the textbox using BElite.Form1.Instance.TestButton1
.
Two notes:
This only works if you always have a single Form1
, for example when Form1
is the main form of your application;
Please note that accessing these controls from a different thread must be done using Control.Invoke()
or Control.BeginInvoke()
. See the documentation on these methods on why and how.
You can access the button using BeginInvoke()
with the following sample:
Form1.Instance.BeginInvoke(new Action(() =>
{
Form1.Instance.TestButton1.Text = "My new text";
}));
Everything in the block ({ ... }
) is safe.