如何在一个用户控件引发事件,并抓住它的炫魅? [英] How do i raise an event in a usercontrol and catch it in mainpage?
问题描述
我有一个自定义用户控件,我建,我需要通知主页(或任何父页面,这个控制是)时,在自定义的用户控件一个按钮被点击我如何提高自定义用户控件和捕捉事件它的主页?我试图用静态的许多建议我去的事件!
I have a Custom Usercontrol that i built and i need to notify Main Page (or any parent page that this control is in) when a button in the custom Usercontrol is clicked how do i raise an event in the Custom usercontrol and catch it in the Main page? i tried using static and many suggested me to go for events!
推荐答案
退房事件冒泡 - <一个href=\"http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx\">http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx
修改添加一个简单的例子:(和更多的编辑,以提高格式化)
Edit to add a quick example: (and additional edit to improve formatting)
的 用户控制的
User Control
public event EventHandler StatusUpdated;
private void FunctionThatRaisesEvent()
{
//Null check makes sure the main page is attached to the event
if (this.StatusUpdated != null)
this.StatusUpdated(new object(), new EventArgs());
}
的 主要页/表格的
Main Page/Form
public void MyApp()
{
//USERCONTROL = your control with the StatusUpdated event
this.USERCONTROL.StatusUpdated += new EventHandler(MyEventHandlerFunction_StatusUpdated);
}
public void MyEventHandlerFunction_StatusUpdated(object sender, EventArgs e)
{
//your code here
}
这篇关于如何在一个用户控件引发事件,并抓住它的炫魅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!