本文介绍了包含按钮的usercontrol可以在click事件上调用自身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
我创建了一个包含按钮的用户控件。我想知道我是否可以在用户控件中点击按钮来调用此用户控件?如果有,怎么样?
谢谢,
Ananya
hiI have created a user control containing a button. I want to know if I can call this user control on button click in the user control? if yes, how?
thanks,
Ananya
推荐答案
/// <summary>
/// Event to indicate UserControl Button Clicked
/// </summary>
public event EventHandler Clicked;
/// <summary>
/// Called to signal to subscribers that UserControl Button Clicked
/// </summary>
/// <param name="e"></param>
protected virtual void OnClicked(EventArgs e)
{
EventHandler eh = Clicked;
if (eh != null)
{
eh(this, e);
}
}
private void button1_Click(object sender, EventArgs e)
{
OnClicked(e);
}
您可以在UserControl中处理自己的事件(但为什么你想要的是另一件事)。只需在构造函数中为上面创建的事件添加处理程序:
You can handle your own events within the UserControl (but why you would want to is another thing). Just add a handler for the event you created above in your constructor:
Clicked += new EventHandler(MyUserControl_Clicked);
...
}
void MyUserControl_Clicked(object sender, EventArgs e)
{
...
}
如果这不包括您要尝试的内容你需要更详细地解释一下。
If this doesn't cover what you are trying to do, you need to explain in better detail.
这篇关于包含按钮的usercontrol可以在click事件上调用自身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!