Click here to Skip to main content
15,867,756 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Hooking Events to Private Members of User Controls

Rate me:
Please Sign up or sign in to vote.
4.60/5 (2 votes)
4 Feb 2010CPOL 13.1K   4   2
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
ehsan237329-Oct-12 16:19
ehsan237329-Oct-12 16:19 
GeneralRe: My vote of 3 Pin
Gordon Kushner30-Oct-12 3:50
Gordon Kushner30-Oct-12 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.