I have two forms, frmMain and frmSettings. I have a label in frmMain
that i would like to change from frmSettings.
So when the user clicks the command button in frmSettings then a value
from a string is used to change the text in the label control on the
frmMain form.
Any help would be appreciated,
thanks!
Start a new windows application.
Add a second form to the project - Form2
Add a button to Form1
Add a label to Form1
Add a button to Form2
Add a private variable to Form2:
private Form1 parentForm;
Create a constructor overload in Form2
public Form2(Form1 parentForm)
{
InitializeComponent();
myParent = parentForm;
}
Add the following code to the Click event of the button on Form1:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 myForm = new Form2((Form1) this);
myForm.Show();
}
Add the following code to the Click event of the button on Form2:
private void button1_Click(object sender, System.EventArgs e)
{
//assuming you can be sure of the indes of
//the control in the controls collection
//which might change if you modify the form
myParent.Controls[0].Text = "Hello World";
//or to be sure you have to right control
//no matter what happens
foreach(Control mycontrol in myParent.Controls)
{
if(mycontrol.GetType() == typeof(Label))
{
if(mycontrol.Name == "label1")
{
mycontrol.Text = "Hello Again";
}
}
}
}
Cheers,
Dave