Jay Duco
Jay Duco

Reputation: 13

call an event in user control using a button in the main form c#

i just want to call a method in my user control when i press a button in my main form. I've tried calling it directly but it doesn't work. here is my sample code

`User control:
 public void replaceText()
    {
        label1.Text = "i'm here";
    }

 Main Form:
 private void button1_Click(object sender, EventArgs e)
    {
        UserControl1 uc = new UserControl1();
        uc.replaceText();
    }`

Upvotes: 1

Views: 43

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

The problem is your instantiating a new user control instance, you're not calling the one you have placed on the main form.

You want to be calling it like this:

private void button1_Click(object sender, EventArgs e)
{
    this.NameOfUserControl.replaceText();
}

Upvotes: 1

Related Questions