Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing events of underlying control

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?

like image 687
Malfist Avatar asked Jul 22 '10 15:07

Malfist


2 Answers

You can forward the events like this:

    public event EventHandler SelectedIndexChanged      {         add { inner.SelectedIndexChanged += value; }         remove { inner.SelectedIndexChanged -= value; }     } 
like image 78
SLaks Avatar answered Sep 19 '22 20:09

SLaks


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.

like image 35
Adam Houldsworth Avatar answered Sep 19 '22 20:09

Adam Houldsworth