简体   繁体   English

UserControl上的公共自定义事件

[英]Public Custom Event on UserControl

I want this: 我要这个:

public partial class ucTest : UserControl  
{
...  
SomeEvent { MessageBox.Show("Inner Call") }
}


public partial class frmTest: Form
{
...  
SomeEvent += OuterEventInstance;
...
void OuterEventInstance(object sender, EventArgs e)
{ MessageBox.Show("Inner Call") }
... 
}

How can I define a public event on a user control that is available (and expandable) in the form that contains an instance of this user control? 如何在包含此用户控件实例的表单中定义可用(和可扩展)的用户控件上的公共事件?

Something like this: 像这样的东西:

public partial class ucTest : UserControl  
{
   public event  EventHandler SomeEvent;

   private void OnSomeEvent()
   {
        EventHandler handler = SomeEvent;
        if(handler != null)
             handler(this, EventArgs.Empty);
   } 

}


public partial class frmTest: Form
{
    public frmTest()
    {
        ucTest uc = new ucTest(); 
        uc.SomeEvent += OuterEventInstance;
    }

    //...

    void OuterEventInstance(object sender, EventArgs e)
    {
        MessageBox.Show("Inner Call")
        //...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM