0

Is it normal for ListBox to fire the DoubleClick event handler method twice when an item is selected? For several days I have attempted to locate what could be causing this and can't seem to isolate any issue that should cause for the Listbox to fire the event handler method twice and am beginning to believe that this could just be the normal response. Does anyone have any experience with this issue or offer any insight?

...
listBox1.Items.Clear();
listBox1.DoubleClick += filteredAlbum_DoubleClick;       
foreach (XmlNode node in replyNode.ChildNodes)
{
    listBox1.Items.Add(node.ChildNodes[0].Value); }
}
listBox1.SelectedIndex = 0;
...

private void filteredAlbum_DoubleClick(object sender, EventArgs e)
{
    var selectedItem = listBox1.SelectedItem.ToString();
    MessageBox.Show(ActiveFilter + " = " + selectedItem);
}

Thanks, Bill

Bill
  • 335
  • 1
  • 4
  • 21

1 Answers1

2

I'd guess you're adding your listBox1.DoubleClick handler twice.

Each event handler will be called once, even if that is actually the same handler added several times. Clearing a listbox does not clear previously bound handlers.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • GSerg, Thank you - I believe that does explain the problem. Is there anyway to clear out a previously bound handler so that there is only one event handling procedure?? Thanks again. – Bill Mar 02 '11 at 18:58
  • @Bill Use the SO search: [Remove all handlers is one go](http://stackoverflow.com/questions/1150250/removing-all-event-handlers-in-one-go). [How to remove all event handlers from a control](http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control). – GSerg Mar 02 '11 at 22:51