Please accept that for valid reasons I need to handle the List Box SelectedIndexChanged event and need the initial stateto beListBox.SelectedIndex = -1;
The followingdoes not do what I need:
ListBox1.DataSource = DataTable1;
ListBoxt.SelectedIndex = -1;
The problem is SelectedIndexChanged event fires twice.
The first time it fires the value of SelectedIndex is 0.
The second time the value of the SelectedIndex is -1.
Please accept that for valid reasons I need to only react to a user selection of index 0. The SelectedIndexChanged event does not know the initial SelectedIndex = 0 was caused by the .DataSource = rather than a user selection.
Tried handling the click even rather than SelectedIndexChanged but the problem there is click does not fire when the user uses the arrow keys
What I have to do
listBox1.SelectedIndexChanged -= new EventHandler( ..
listBox1.DataSource = dt;
if
(listBox1.SelectedIndex != -1) listBox1.SelectedIndex = -1;
listBox1.SelectedIndexChanged += new EventHandler( ..
But from a support perspective I don't like removing and adding the EventHandler
Is the a better way to do this?
Microsoft why assign SelectedIndex = 0 with .DataSource = statement. What is that basis for that assumption? I think a more valid (and less problematic) assumption is SelectedIndex = -1. When the ".DataSource =" statmentsets the SelectedIndex = 0 itcan causeconfusion (was it a user action)for the event handler.
Thanks