Hooking Events to Private Members of User Controls






4.60/5 (2 votes)
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.