我有一个User Control
包含一堆控件。我想设置这个的默认事件User Control
to the Click
我的一个按钮的事件。
我知道为了将默认事件设置为 UserControl 的事件之一,我应该添加以下属性:
[DefaultEvent("Click")]
public partial class ucPersonSearch : UserControl
...
我想知道是否可以做类似的事情:
[DefaultEvent("btn1_Click")]
public partial class ucPersonSearch : UserControl
...
我想在单击 btn1 时触发托管此用户控件的表单中的一些方法。
这确实是我项目中的一个结,您的回答将很有价值。
您不能将班级成员的事件暴露给班级外部。其他人如何订阅Click
的事件Button
在你的里面UserControl
?你尝试过吗?除非您使按钮可以从外部访问,否则这是不可能的,这不好(每个人都可以更改所有属性)。
您必须定义一个新事件,并在所需事件(单击按钮)发生时触发新事件:
[DefaultEvent("MyClick")]
public partial class UCPersonSearch : UserControl
{
Button btnSearch;
public event EventHandler MyClick;
public UCPersonSearch()
{
btnSearch = new Button();
//...
btnSearch.Click += new EventHandler(btnSearch_Click);
}
void btnSearch_Click(object sender, EventArgs e)
{
OnMyClick();
}
protected virtual void OnMyClick()
{
var h = MyClick;
if (h != null)
h(this, EventArgs.Empty);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)