Geeks With Blogs

The Wrecking Bawl Destructuring query language, one keyword at a time.

I don't know if this would be considered a bug or not, or if I'm just missing something, but when you use ListBox.DataSource in Windows.Forms (.Net 2.0), and you add or remove an item from an ArrayList that is set as the DataSource, there is no method to refresh the listbox.  The only thing I know that works is setting the DataSource to null and then setting it back to the ArrayList.  That just doesn't seem right. 


addendum (the next day):  I may have found another bug!  When I move an item up or down in my ArrayList and then rebind it to the ListBox, I still want the item that was moved to be selected.  I tried ListBox.ClearSelected() followed by ListBox.SelectedIndex = originalIndex, but that resulted in both the zero-index item being selected and the original item being selected.  I ended up having to do the following line twice in a row before setting the selection:

ListBox.SelectedIndex = -1;

Posted on Wednesday, January 17, 2007 1:15 AM all tech stuff , C# | Back to top

Comments on this post: ListBox bug in C#?

# re: ListBox bug in C#?
Requesting Gravatar...
THX. that helped me a lot. this "bug" did drive me nuts :D
Left by Ingo Fabbri on Oct 16, 2007 8:51 AM

# re: ListBox bug in C#?
Requesting Gravatar...
What I found you have to do on a multi-select list box to set the SelectedIndex is this:


This will set the selected index as well as select it on the screen. Change the SetSelected value to false and it will set the selected index, however, will not select the item on the screen.
Left by Marko on Feb 05, 2008 4:24 AM

# re: ListBox bug in C#?
Requesting Gravatar...
ListBox will bind correctly to an instance of System.ComponentModel.IBindingList such as System.ComponentModel.BindingList<T>. If your design is tightly bound to using an ArrayList, try creating a subclass of ArrayList that implements IBindingList.
Left by Alex Humphrey on Jun 26, 2008 7:39 PM

# re: ListBox bug in C#?
Requesting Gravatar...
I have populated data to a ListBox using dataView (sql information) and bind the listbox, and now im having a problem with changing the indexValue from the listbox and save it back to the database after positioning items. I have managed to move the items up and down but item values doesnt change. For example i want to be able to move items up and down and save them back with their new position.

Can somebuddy please help me on this?
Can you also notify me on my email of the answer?
Left by Lucious on Mar 22, 2009 11:30 PM

Your comment:
 (will show your gravatar)

Copyright © Alex Bransky | Powered by: