Click here to Skip to main content
Click here to Skip to main content

Hooking Events to Private Members of User Controls

By , 4 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

Gordon Kushner
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 Pinmemberehsan237329-Oct-12 16:19 
GeneralRe: My vote of 3 PinmemberGordon Kushner30-Oct-12 3:50 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 4 Feb 2010
Article Copyright 2010 by Gordon Kushner
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid