Groupbox check for changes event

phadaphunk picture phadaphunk · Aug 2, 2012 · Viewed 8.1k times · Source

I have a DataGridView and a GroupBox control containing a few ComboBoxes.
Depending on what is selected in the ComboBoxes, the elements in the grid changes.

Is there a way to say

If (Something Changes Within The GroupBox)
{
   //Update the grid
}

(Without writing a OnSelectedIndexChange event for every boxes)
I don't want the code for the updating part, I just need an event or something I could use to check if a the value of a control has changed within the GroupBox.
Any Idea ?


Update

Ok I think I didn't explained it the right way.
Forget about the ComboBox.
Let's say I have a bunch of controls in a GroupBox is there a way to say :

As soon as the value of one of the control changes, create an event.

Answer

Dave New picture Dave New · Aug 2, 2012

You could hook up each combo box SelectedIndexChanged event to one method:

comboBox1.SelectedIndexChanged += new System.EventHandler(GroupBoxComboBoxChange);
comboBox2.SelectedIndexChanged += new System.EventHandler(GroupBoxComboBoxChange);
comboBox3.SelectedIndexChanged += new System.EventHandler(GroupBoxComboBoxChange);
comboBox4.SelectedIndexChanged += new System.EventHandler(GroupBoxComboBoxChange);

Or using LINQ to setup an event handler for any combo box selection change:

GroupBox.Controls.OfType<ComboBox>.ForEach(cb => cb.SelectedIndexChanged += new System.EventHandler(GroupBoxComboBoxChange));

Answer to your update: You are looking for a ControlValueChanged() event. I think the problem here is that all controls are different. What defines a "ValueChanged" event for a ComboBox isn't necessarily the same for a TextBox. It would be a semantic challenge and not very clear. Hope this makes sense.