How to change the properties of a Form being inside a Usercontrol that is not in the Form? C#

Asked

Viewed 155 times

0

In my code I am inside a Usercontrol that is not inside the Form and I would like to change the properties of my Form through this Usercontrol. Example of my code:

private void btn_Click(object sender, EventArgs e) //Botão dentro do UserControl
{
  Form1 frm1 = new Form1();
  frm1.label.Text = "Changed";
}

I put the Label component that is in Form1 as Public, I installed the Form1 inside my Usercontrol and I finished the component, changing its text, but I click and no change happens in Form1, so my question is, how can I change the properties of a Form through a Usercontrol that is not in this Form.

1 answer

1


You need the instance of yourself Form1 (for example, passed by parameter to the usercontrol), an instantiation created within your usercontrol is a different object than the one already created.

Another option would be to use the property OpenForms:

Form1 frm1 = (Form1)Application.OpenForms["Form1"];
frm1.label.Text = "Changed";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.