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?
You can forward the events like this:
public event EventHandler SelectedIndexChanged { add { inner.SelectedIndexChanged += value; } remove { inner.SelectedIndexChanged -= value; } }
You will need to code these into the control yourself - the user control does not automatically promote the events of its child controls. You can then cross-wire your actual control to the user control's promoted event:
public event EventHandler SelectedIndexChanged; private void OnSelectedIndexChanged(object sender, EventArgs e) { if (SelectedIndexChanged != null) SelectedIndexChanged(sender, e); } public UserControl1() { InitializeComponent(); cb.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged); }
Unfortunately you will need to do this for every event you are interested in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With