Click here to Skip to main content
15,895,142 members

Updating Listbox Items when they are custom Objects

Revision 1
I have a ListBox on a form.

When I add a new item to the ListBox, I am adding a custom class called Query. In the Query class I have overridden the ToString() method to display the Name property.

This works great when I add all of the new items. They are stored correctly in the ListBox and they are displayed correctly.

My problem comes when I try to rename a Query. I have a ContextMenuStrip that I use and a simple InputBox to allow the user to rename the item. It works properly and renames the Name property of the Query, however, it does not update what is displayed in the ListBox.

Example:
When I click the "Add" button, it adds a new Query with the name "New Dataset". Then, the user right-clicks on that item and they click the "Rename" button. This loads up an InputBox asking them to enter a new name and loads the original name into the InputBox.

The user enters a new name hits enter and I run:
C#
//In C#
(MyMDX.Query)lboDataSets.SelectedItem.Name = newName;

VB
'In VB.NET
CType(lboDataSets.SelectedItem, myMDX.Query).Name = newName


This does update the SelectedItem to reflect the new name, however, the displayed value of the Item in the ListBox is still "New Dataset".

I have tried running lboDataSets.Invalidate(), lboDataSets.Refresh(), lboDataSets.BeginUpdate()/lboDataSets.EndUpdate and I have tried changing lboDataSets.SelectedValue() and nothing updates the displayed value.

So, how can I get the displayed value to update?
Posted 6-Apr-10 10:39am by William Winner.
Tags: ,