使用主窗体中的按钮调用用户控件中的事件 C#
call an event in user control using a button in the main form c#
我只想在我按下主窗体中的按钮时调用我的用户控件中的方法。我试过直接调用它,但它不起作用。
这是我的示例代码
`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();
}`
问题在于您实例化了一个新的用户控件实例,而没有调用放置在主窗体上的那个实例。
您想这样称呼它:
private void button1_Click(object sender, EventArgs e)
{
this.NameOfUserControl.replaceText();
}
我只想在我按下主窗体中的按钮时调用我的用户控件中的方法。我试过直接调用它,但它不起作用。 这是我的示例代码
`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();
}`
问题在于您实例化了一个新的用户控件实例,而没有调用放置在主窗体上的那个实例。
您想这样称呼它:
private void button1_Click(object sender, EventArgs e)
{
this.NameOfUserControl.replaceText();
}