当我在主窗体中按下按钮时,我只想在用户控件中调用一个方法。我尝试过直接调用它,但是它不起作用。这是我的示例代码
`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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句