How to Refresh a ListBox Contents ?
-
Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?
-
Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?
Maybe my thinking here is wrong, but how about simply clearing the listbox and re-add your object collection?
I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
-
Maybe my thinking here is wrong, but how about simply clearing the listbox and re-add your object collection?
I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
Ok, thanks. I have just tried and it works.
-
Ok, thanks. I have just tried and it works.
Great :)
I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
-
Ok, thanks. I have just tried and it works.
IMO removing and adding the collection is too much effort and causes flickering. Invalidate() should be sufficient, unless you are in an event handler and want an immediate repaint, if so use Refresh(). If that does not work for you, then something is wrong and you may want to show some code. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?
The displayed text does change for my business object collection when bound to a list box. Are you setting the items manually (using Add) or you set the DataSource to your object collection? Which interface did you implemented on the collection? (IList, BindingList, ICollection etc)? Also check out the ListChangedEventArgs[^]