Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > ListBox.DataSource = causes SelectedIndex = 0 and I need SelectedIndex = -1
 

ListBox.DataSource = causes SelectedIndex = 0 and I need SelectedIndex = -1

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

Gymmie Dean  Saturday, May 31, 2008 5:47 PM

Hi Gymmie,

This is a default and by design behavior that whena ListBox's DataSource property is set, the first item in the ListBox is selected and the SelectedIndex property is set to 0.

The basic assumption of this behavior is that after a ListBox is populated, the first item should be selected.

If you don't like it, you can set the SelectedIndex property of the ListBox to -1 after you set the DataSource property of the ListBox.

> But from a support perspective I don't like removing and adding the EventHandler. Is the a better way to do this?

Your workaround is practical and there isn't any better way to do what you want.

Hope this helps.

Thanks,

Linda

Linda Liu  Tuesday, June 03, 2008 8:30 AM

Hi Gymmie,

This is a default and by design behavior that whena ListBox's DataSource property is set, the first item in the ListBox is selected and the SelectedIndex property is set to 0.

The basic assumption of this behavior is that after a ListBox is populated, the first item should be selected.

If you don't like it, you can set the SelectedIndex property of the ListBox to -1 after you set the DataSource property of the ListBox.

> But from a support perspective I don't like removing and adding the EventHandler. Is the a better way to do this?

Your workaround is practical and there isn't any better way to do what you want.

Hope this helps.

Thanks,

Linda

Linda Liu  Tuesday, June 03, 2008 8:30 AM
You can use a datagrid instead of a listbox, which doesn't auto select any records. I thought I'd have to use that kludge. Made me sick to my stomach. The built in list box should be renamed the "pickoneitembox"
  • Proposed As Answer bybpeikes Tuesday, August 18, 2009 6:12 PM
  •  
bpeikes  Tuesday, August 18, 2009 6:12 PM

You can use google to search for other answers

Custom Search

More Threads

• data grid view validations
• Combo box- How can I display two field from the table? (VB.net or VB visual studio 2005)
• Font problems with DataGridView component?
• Dynamically created GridView â€?use update postback
• Last row blank when DataGridView VirtualMode = true
• DataGridViewComboBoxCell with multiple selection ?
• How to wrap a row in the DataGridView?
• DataGridView Update to Source Database
• Hierarchical grid and self referencing table
• data binding to multiple windows forms