Why not simple; how to change listbox selected item text

  • Thread starter Thread starter Amil
  • Start date Start date
A

Amil

I've added items to a listbox by calling ListBox.Items.Add(new MyObject()),
where MyObject has overridden ToString to show each item's display value.

I can't seem to change the DISPLAYED TEXT when I change the object behind
the selected index:

((MyObject)listbox.SelectedItem).mydisplayproperty = "abcdef";

The ListBox display doesn't change, but I know the object is changed since
other buttons that examine the contents of each listbox item see it changed
(and enable/disable their button state). How can I easily do this? I've
tried Refresh, Invalidate, Update methods on the listbox. No luck.

Amil
 
Hi Amil,

I'm not sure how to force ListBox to reread its items, but you can simulate the effect by adding and removing the item from the list

MyItem m = (MyItem)listBox1.SelectedItem;
m.MyText = "ABCD";
listBox1.Items.Insert(listBox1.SelectedIndex, m);
listBox1.Items.RemoveAt(listBox1.SelectedIndex);

The procedure shouldn't be noticable to the user.
 
Back
Top