Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupbox check for changes event

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.

like image 908
phadaphunk Avatar asked Mar 25 '26 16:03

phadaphunk


2 Answers

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.

like image 177
Dave New Avatar answered Mar 28 '26 05:03

Dave New


There is no "something inside me changed" for GroupBoxes, but you can "cheat" and DYI like this (it's just a proof-of-concept without error checking and the sort):

// In a new Windows Forms Application, drop a GroupBox with a ComboBox and a CheckBox inside
// Then drop a TextBox outside the ComboBox. Then copy-paste.

// this goes somewhere in your project
public static class handlerClass
{
    public static string ControlChanged(Control whatChanged)
    {
        return whatChanged.Name;
    }
}

// And then you go like this in the Load event of the GroupBox container
void Form1_Load(object sender, EventArgs args)
{
    foreach (Control c in groupBox1.Controls)
    {
        if (c is ComboBox) 
            (c as ComboBox).SelectedValueChanged += (s, e) => { textBox1.Text = handlerClass.Handle(c); }; 
        if (c is CheckBox) 
            (c as CheckBox).CheckedChanged += (s, e) => { textBox1.Text = handlerClass.Handle(c); }; }
    }
}

Since every Control has its own "I'm changed!" kind of event, I don't think it can be any shorter as far as boilerplate goes. Behavior is a mere sample that writes the name of the control that changed in a ComboBox

like image 36
Alex Avatar answered Mar 28 '26 06:03

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!