Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a ListBox refresh its item text?

I'm making an example for someone who hasn't yet realized that controls like ListBox don't have to contain strings; he had been storing formatted strings and jumping through complicated parsing hoops to get the data back out of the ListBox and I'd like to show him there's a better way.

I noticed that if I have an object stored in the ListBox then update a value that affects ToString, the ListBox does not update itself. I've tried calling Refresh and Update on the control, but neither works. Here's the code of the example I'm using, it requires you to drag a listbox and a button onto the form:

Public Class Form1      Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)         MyBase.OnLoad(e)          For i As Integer = 1 To 3             Dim tempInfo As New NumberInfo()             tempInfo.Count = i             tempInfo.Number = i * 100             ListBox1.Items.Add(tempInfo)         Next     End Sub      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         For Each objItem As Object In ListBox1.Items             Dim info As NumberInfo = DirectCast(objItem, NumberInfo)             info.Count += 1         Next     End Sub End Class  Public Class NumberInfo      Public Count As Integer     Public Number As Integer      Public Overrides Function ToString() As String         Return String.Format("{0}, {1}", Count, Number)     End Function End Class

I thought that perhaps the problem was using fields and tried implementing INotifyPropertyChanged, but this had no effect. (The reason I'm using fields is because it's an example and I don't feel like adding a few dozen lines that have nothing to do with the topic I'm demonstrating.)

Honestly I've never tried updating items in place like this before; in the past I've always been adding/removing items, not editing them. So I've never noticed that I don't know how to make this work.

So what am I missing?

like image 949
OwenP Avatar asked Sep 14 '08 15:09

OwenP


People also ask

How do I update items in ListBox?

There areal so three buttons to manipulate the content of the listBox. Pressing the first button will append a new item to the end of the list. Pressing the second button will insert the new item before the currently selected item. Pressing the third button will delete the currently selected item from the list.

How do I get text from ListBox?

string value = listBox. Items[index]. ToString(); If the things in the listbox are some sort of object, you may need to override ToString() to get the desired result, or cast the thing you get out of the listbox to the desired type and then access an appropriate property.

How do I know if a ListBox item is selected?

you can determine the selected item(s) in the ListBox control by enumerating the Items collection and testing the Selected value for each ListItem element.

What method can we use to add the items into the ListBox?

To insert an item into the list box at a specific position, use the Insert method. To add a set of items to the list box in a single operation, use the AddRange method.


2 Answers

I use this class when I need to have a list box that updates.

Update the object in the list and then call either of the included methods, depending on if you have the index available or not. If you are updating an object that is contained in the list, but you don't have the index, you will have to call RefreshItems and update all of the items.

public class RefreshingListBox : ListBox {     public new void RefreshItem(int index)     {         base.RefreshItem(index);     }      public new void RefreshItems()     {         base.RefreshItems();     } } 
like image 186
Brad Bruce Avatar answered Sep 28 '22 03:09

Brad Bruce


lstBox.Items[lstBox.SelectedIndex] = lstBox.SelectedItem; 
like image 23
Elton M Avatar answered Sep 28 '22 01:09

Elton M