Exposing events of underlying control

前端 未结 4 1611
北恋
北恋 2020-12-01 08:54

I have a combobox in a custom control. How can I expose specific events from it such as SelectedIndexChanged or KeyPress, etc to anyone/thing implementing my custom control?

4条回答
  •  再見小時候
    2020-12-01 09:27

    A very simple solution rather than having custom events, would be to expose the nested control as a property of the custom control. From there you could attach event handlers to it very easily. It is not always advisable to expose child controls, but depending on the control type and how you are using it, it may work.

    //create an instance of my control
    MyCustomControl controlInstance = new MyCustomControl();
    
    //attach and event handler to the exposed subcontrol
    controlInstance.SaveButton.Click += new EventHandler(SaveButton_Click);
    

提交回复
热议问题