我如何在一个用户控件中引发一个事件,并在主页中捕获它? [英] How do i raise an event in a usercontrol and catch it in mainpage?

查看:157
本文介绍了我如何在一个用户控件中引发一个事件,并在主页中捕获它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl ,我需要通知父页面 UserControl 中的按钮被点击。如何在 UserControl 中提出一个事件,并在主页上抓住它?我尝试使用 static ,许多人建议我去参加活动!

I have a UserControl, and I need to notify the parent page that a button in the UserControl was clicked. How do I raise an event in the UserControl and catch it on the Main page? I tried using static, and many suggested me to go for events!

推荐答案

p>查看活动冒泡 - http:// msdn .microsoft.com / en-us / library / aa719644%28vs.71%29.aspx

Check out Event Bubbling -- http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx

编辑添加一个快速示例: / strong>(和其他修改以改善格式)

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(this, 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆