If you are sure that there will be at most one instance of the form, you could create a static property exposing the text box's text. In order to do this, you need a static reference to the form itself (I call the form InfoForm
, its more informative than Form1
).
For this purpose I add a static Open
method. If the form is open alredy it brings it to foreground, otherwise it opens it and assigns it to the static field _instance
.
public partial class InfoForm : Form
{
public InfoForm()
{
InitializeComponent();
}
private static InfoForm _instance;
public static InfoForm Open()
{
if (_instance == null) {
_instance = new InfoForm();
_instance.Show();
} else {
_instance.BringToFront();
}
return _instance;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Make sure _instance is null when form is closed
_instance = null;
}
// Exposes the text of the infoBox
public static string InfoText
{
get { return _instance == null ? null : _instance.infoBox.Text; }
set { if (_instance != null) _instance.infoBox.Text = value; }
}
}
Now you can open the form and access its infoText
TextBox
like this
InfoForm.Open();
InfoForm.InfoText = "Hello";
string msg = InfoForm.InfoText;