0

我要这个:

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") }
... 
}

如何在包含此用户控件实例的表单中定义可用(且可扩展)的用户控件上的公共事件?

4

1 回答 1

2

像这样的东西:

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")
        //...
    }
}
于 2013-02-21T08:47:12.507 回答