Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why not simple; how to change listbox selected item text

3 views
Skip to first unread message

Amil

unread,
May 12, 2005, 4:54:33 PM5/12/05
to
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


Morten Wennevik

unread,
May 13, 2005, 4:28:58 AM5/13/05
to
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.

--
Happy coding!
Morten Wennevik [C# MVP]

0 new messages