3

As it says in tilte:

  • there is a ListBox
  • when I select a value in
  • 1st SelectedValueChanged event contains value I clicked on
  • and time to time there is 2nd SelectedValueChanged event contains 1st value from the list

I catched it adding those piece of code:

This is what my designer contains:

    this.listBox1.SelectedValueChanged += 
new System.EventHandler(this.listBox1_SelectedValueChanged);

This is what my class contains :

public class myControl : UserControl
{

  private void listBox1_SelectedValueChanged(object sender, EventArgs e)
  {
     StackTrace st = new StackTrace(true);
  }                                    /// I put breakpoint that line to look at st
}

I cannot see where 2nd event is fired. In the stack there is plenty of system code and nothing very revelant from my own code. But I can past it here on demand.

The only thing I'm not really mastering is the UserControl context. Maybe something to do around that.

With that, do you have an idea about why I got twice call when I click once ?

edit after King King's question:

scenario is:

  • affecting listBox1.DataSource calls SelectedValueChanged

    1. in listBox1_SelectedValueChanged sender: {SelectedItem = "1st value"} object {System.Windows.Forms.ListBox}
  • clicking to 3rd value in listBox1 calls twice SelectedValueChanged

    1. in listBox1_SelectedValueChanged sender: {SelectedItem = "3rd value"} object {System.Windows.Forms.ListBox}
    2. in listBox1_SelectedValueChanged sender: {SelectedItem = "1st value"} object {System.Windows.Forms.ListBox}
  • after that only 1st value is selected

mickro
  • 881
  • 2
  • 11
  • 26
  • 2
    You posted fairly many words but I still don't get well what your problem actually is? Do you mean you just select `1` value (by 1 click) in your `listBox` and the `SelectedValueChanged` is fired **twice**? Or you select **the same value** by 2 clicks (or more) but the `SelectedValueChanged` is fired twice (or more)? – King King Sep 06 '13 at 04:40
  • `SelectedValueChanged` is fired once when I affect DataSource. Then first click at any value (ie 3rd value) in list fires twice `SelectedValueChanged`. 1st call contains 3rd value and 2nd call contains 1st value. – mickro Sep 06 '13 at 04:54
  • for your update, I strongly doubt that your code in `SelectedValueChanged` event handler may cause the problem, in that case, debugging will make everything clear. BTW, what if you click on another item but `the third item`? Anyway, it's obviously that, when you click on the `3rd item`, it of course fires the `SelectedValueChanged`, but **for some reason**, the `1st item` is selected then and fires the `SelectedValueChanged` one more time, and as you said, after all the `1st item` is selected (not the `3rd item`). – King King Sep 06 '13 at 06:38
  • @KingKing "I strongly doubt that your code in `SelectedValueChanged` event handler may cause the problem" Yes I agree. I added this to try to understand what is happening. – mickro Sep 08 '13 at 21:10
  • @KingKing "what if you click on another item but the third item?" exactly same scenario – mickro Sep 08 '13 at 21:11
  • @KingKing "but **for some reason**, the 1st item is selected then and fires the `SelectedValueChanged`", yes it is what I want to figure out. – mickro Sep 08 '13 at 21:13
  • My `UserControl` is pretty empty and contains one textbox, one button, one listbox. Certainly good time to test it alone (without my main project around). – mickro Sep 08 '13 at 21:42
  • I believe it's related to this. http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox(v=vs.110).aspx OnSelectedIndexChanged Raises the SelectedValueChanged event. OnSelectedValueChanged Raises the SelectedValueChanged event. Both events occur when you click on other value. Basically you change index first, then you get the SelectedValueChanged. Why don't you just use SelectedIndexChanged? – R Quijano Jan 14 '14 at 19:38

1 Answers1

0

Sounds like you have a an issue with 2 or more other events, either you are selecing the third element and the reloading the listbox or something like that. Sorry I do not have Comment rights as of yet, But can you select the first then the third and then select the 1st and do you get double events after you select the first for the second time?

Lawrence Thurman
  • 657
  • 8
  • 15