65.9K
CodeProject is changing. Read more.
Home

Hooking Events to Private Members of User Controls

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (2 votes)

Feb 4, 2010

CPOL
viewsIcon

13496

Say you need capture an event for a control in a User Control and you are not permitted to change the control itself.You can hook into control events, even for private controls with this code://In a Windows Form//Get the private control cboSomePrivateControl from the user control's...

Say you need capture an event for a control in a User Control and you are not permitted to change the control itself. You can hook into control events, even for private controls with this code:
//In a Windows Form

//Get the private control cboSomePrivateControl from the user control's Control collection
ComboBox cbo = (customUserControl.Controls["cboSomePrivateControl"] as ComboBox);

//Hook  control change and assign a method
cbo.SelectedIndexChange += new EventHandler(cbo_SelectionChange);

...

//create the method to handle the change.
void cbo_SelectionChange(object sender, EventArgs e)
{

}
It's that easy.