Why not simple; how to change listbox selected item text

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
 
M

Morten Wennevik

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top