I have a textbox on a Windows Forms UserControl that I am trying to update the contents of from elsewhere in my solution.
I am dynamically adding the UserControl to my form, and I have a static Instance property on the UserControl so that I can access it from a referencing library.
I have excluded double-lock checking here for brevity...
public partial class MyForm : Form {
private MyControl ctl;
public MyForm() {
ctl = new MyControl();
MyControl.Instance = ctl;
Controls.Add(ctl);
}
}
public partial class MyControl : UserControl {
public static MyControl Instance;
public void LogMessage(string msg) {
if (MyInnerTextBox.InvokeRequired) {
MyInnerTextBox.Invoke(LogMessage, msg);
return;
}
MyInnerTextBox.AppendText(msg);
MyInnerTextBox.Refresh();
this.Refresh();
}
}
When I call MyControl.Instance.LogMessage("blah")
from another class, it appears that my text is added to another instance of the MyControl control... not the instance on the form.
What am I missing? Shouldn't the static instance reference allow me to reference the instance of MyControl on MyForm?