Listbox problems

  • Thread starter Thread starter Karin
  • Start date Start date
K

Karin

Hi,
Can anyone explain to me why SelectedIndex is automatically changed to 0
when I set it to -1?

This happens when I set SelectedIndex = -1 from the click-eventhandler of a
button. My ListBox is bound to an ArrayList.

Thanks! /K
 
Hi Karin,

Check your handler for SelectedIndexChange , the problem may be there.

Hope this help,
 
Hi,
I thought so first too so I made a very simple form with just one listbox
and a button to try it out without the eventhandler for SelectedIndexChanged
but the problem was still there.
Strange thing is that it works as expected when SelectedIndex = 0 before the
button is clicked.
I have found a way to avoid the problem which I don't really like but it
works...

private bool blnNew = false;
private void lb_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (lb.SelectedIndex != -1 )
{
if (blnNew)
lb.ClearSelected();
}
private void button1_Click(object sender, System.EventArgs e)
{
blnNew=true;
lb.ClearSelected();
blnNew=false;
}

/K
 
Back
Top