0

I have a ComboBox with items in it. I also have a SelectedIndexChange event. When I open the ComboBox and hover over an item, the SelectedIndex property seems to change to that item. I'd only like it to change when I click on the item. Is it possible to disable that behavior?

I have a timer that refreshes an Image based on SelectedIndex of ComboBox, but still, even if I highlight an item but don't select, why does the Image changes while it should not change and it only should change when I select an item.

5
  • I have an Image in my program that changes the Image's 'Image' property's value with the combobox. When I highlight the item, the Image changes to that value for some reason. I only want to do that when I click on the item. (also, when highlighting an item, it seems to skip some code or something, as it does not display the panel it's supposed to display and goes on to calling loadImage() ) Commented Nov 13, 2015 at 16:42
  • Can you debug application by setting breakpoint in SelectedIndexChange event handler and hovering over ComboBox ? Does breakpoint hit? Commented Nov 13, 2015 at 16:45
  • No, it only triggers when I click. I figured out a part of the problem, I have a timer that refreshes the image every x seconds. But still, even if I highlight an item, why does the image change to that value? It should not, it only should when I click. (also the timer's event is set to refresh the image to the value of the combobox.SelectedIndex) Commented Nov 13, 2015 at 16:50
  • what the timer does is image.LoadAsync(Properties.Settings.Default.image_urls[combobox.SelectedIndex]); Commented Nov 13, 2015 at 16:54
  • 1
    SelectedIndexChanged doesn't fire but the SelectedIndex really changes. Commented Nov 13, 2015 at 17:07

1 Answer 1

3

The Problem

When the the mouse moves on items of ComboBox, the SelectedIndex changes but SelectedIndexChanged event doesn't fire, so in your timer Tick event, you will see the change while SelectedIndexChanged doesn't fire.

Scenario of reproducing the problem

To simply reproduce the problem, put a Timer on a form and enable it, then handle its Tick event. Also add a ComboBox and add some items to it and handle its SelectedIndexChanged event. When you open the dropdown and move mouse over items, you will see the Text of form changes to the index of item that is under the cursor, while SelectedIndexChanged doesn't fire and no MessageBox will show.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(this.comboBox1.SelectedIndex.ToString());
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.Text = this.comboBox1.SelectedIndex.ToString();
}

The Solution for your case

You can simply check if the dropdown is not open using DroppedDown property of ComboBox, then do the job:

private void timer1_Tick(object sender, EventArgs e)
{
    if(!this.comboBox1.DroppedDown)
        this.Text = this.comboBox1.SelectedIndex.ToString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

SelectedIndex changing on mouse hover - Is this a .NET bug? Or is this behavior documented somewhere?
@pius I am also very interested in whether the behavior is documented. IMO this "problem" could definitely be used on the side of good as I have managed to do here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.