Fire comboBox SelectedIndexChanged only from user click

38,435

Solution 1

You can use both methods You proposed:

  1. use boolean variable
  2. detach event method, populate combobox, attach event method like this

    my_combo.SelectedIndexChanged -= my_Combo_SelectedIndexChanged;
    populateCombo();
    
    my_combo.SelectedIndexChanged += my_Combo_SelectedIndexChanged;
    

my_Combo_SelectedIndexChanged is the name of method you attached to the event.

Solution 2

I have done it a lot number of times. Solution1: Delete EventHandler from designer. Populate the combobox and then set EventHandler.

Combo1.SelectedIndexChanged += new EventHandler Combo1_SelectedIndexChanged;

But it will work only if you are populating the combobox once.If you are doing it for many number of times, then you may be in a mess.

Solution2: Its my preference and I use it regularily. Change your selection change event as:

private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
   ComboBox cb = (ComboBox)sender;
   if(!cb.Focused)
   {
      return;
   }
   // Here is your Code for selection change
}

So now the event will be fired only if its in focus. Hope you were looking for the same. Hope it Helps

Solution 3

Not sure if this is any use now but I found this answer, which seems cleaner to me.

From the MSDN Library - ComboBox.SelectionChangeCommitted Event

"SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically."

Solution 4

I would use control.ContainsFocus instead of creating other bool. The caveat here is that you have to make sure the user has focus on that control. Either by key or mouse.

if(combo.ContainsFocus){ MyEventLogic();}

Solution 5

  1. Solution: If you're populating combobox with static values only ones, just populate them and after subscribe to event from code. Do not use WinForms Designer to subscribe to it.
  2. If it's not possible during loading can:

    a) define a boolean variable bool loading, set it to true before you begin to populate combo with data, and in event handler check

    if(loading) return;

    b) Unsubsribe from event:

    If subscription was:

    comboBox.SelectedIndexChanged += delegate(...);

    Unsubscription before you begin load data is:

    comboBox.SelectedIndexChanged -= delegate(...);

As loading of data finished, subscribe again.

Share:
38,435
micahhoover
Author by

micahhoover

Updated on July 05, 2022

Comments

  • micahhoover
    micahhoover almost 2 years

    I wrote a method to handle a comboBox's SelectedIndexChanged event.

    In the constructor I populated the comboBox, and this activated my event-handling method. Which I don't want since nobody clicked on the comboBox.

    Is there an easy way to get the comboBox not to fire the event unless the user clicked it?

    If that is not possible, is there a way to disconnect the event to the method temporarily? Could I just set "my_combo.SelectedIndexChanged = null" and then create a new System.EventHandler?

    Or I guess I could create some kind of boolean member variable that I can switch on or off and put a branch check in my method. That seems like a kludge, though.

  • micahhoover
    micahhoover over 12 years
    Up vote for creativity. I've used this solution on other frameworks (Qt), but it ended up leading to some bad side effects. Was still the best solution in that case. Here, probably not.
  • micahhoover
    micahhoover over 12 years
    You seemed to understand the question since you sort of returned it back to me, but echoing the question is not exactly what I was looking for here. No up vote.
  • Tigran
    Tigran over 12 years
    @micahhoover: honestly don't see any difference between mine answer and those ones I see in the list. I didn't ask you any question, but just provide with couple of solutions I think can be suitable to you. By the way, no prob, good luck.
  • psyklopz
    psyklopz about 11 years
    This is by far the simplest method. It does what the user wants without having to create & destroy event handlers. It also encourages better programming practices, where using a boolean to synchronize events in spite of other events is dangerous.
  • cesarse
    cesarse about 11 years
    Actually, the event must be triggered whether the component is not focused. Just invert the boolean condition... ;-)
  • Cody
    Cody about 10 years
    I also agree. This is simple and works effectively. The accepted answer did not work for me, and it does seem messy compared to this solution.
  • Ahmad
    Ahmad over 8 years
    When it fires, the comboBox.Text has its old value.
  • user692942
    user692942 over 6 years
    @Ahmad what does that mean? Can't say I've had any problems with it myself.
  • codingcoding
    codingcoding almost 6 years
    This is nice. I searched for something like "SelectedIndexChanging" and the consensus seemed to be that it was not an option. Well, here it is.
  • Amit Lipman
    Amit Lipman almost 6 years
    Greate solution! if you change the casting to Control its generic for any type of control..
  • Elmue
    Elmue over 5 years
    This is not a good idea. This is not bullet proof. What if you call for example a function from a timer which reloads the combobox while the combobox STILL has the focus from a previous click?