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

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

问题描述

我有一个 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.

推荐答案

查看事件冒泡 -- http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx

示例:

用户控制

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());
}

主页/表单

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天全站免登陆