I've been trying to send a string from one form named mainForm to my other form unlockForm.
The references I've found are sending values from things like textboxes. I simply want to send a string.
I've been trying to send a string from one form named mainForm to my other form unlockForm.
The references I've found are sending values from things like textboxes. I simply want to send a string.
This is done in two ways. If you are trying to pass a string from the owner form to the child form, you can do it in a parameter, like so:
class Owner : Form
{
private Child Child;
public Owner()
{
Child = new Child("Value To Pass");
}
}
class Child : Form
{
public Child(string value)
{
//do something with value
}
}
If you want to pass from the Child to the Owner, it would be like this:
class Owner : Form
{
public Owner()
{
Child = new Child();
Child.ShowDialog();
string childValue = Child.Value;
}
}
class Child : Form
{
public string Value{get;set;}
public Child()
{
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
value = "Value To Set";
this.Close();
}
}
Note that I'm using "Child.ShowDialog()" to make sure that the child form is closed before the value returns. This isn't necessary, but safer.