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
 

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

Similar Threads

Overriding a property is not working. 2
ListBox oddity 8
listbox 1
listview vs listbox? 3
macro substitution in C# 2
Binding listbox items 1
Listbox SelectedIndex set to -1 3
How to remove listbox flashing 11

Back
Top