Listbox problems

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
 
I

Ignacio Machin

Hi Karin,

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

Hope this help,
 
K

Karin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top