Hello,

I've run into a brick wall in working with a standard ListBox. I'm loading
lightweight objects into a ListBox component - call them Elements. The
Element class has an ElementName property and an overridden ToString() method
that displays the ElementName. When I load Elements into my list box Items
collection, the ElementName displays and all is well. My problem occurs
when I change an ElementName property programatically. The underlying
Element does indeed change, but I can't get the text displayed the list box
to reflect it! I can delete and re-add the Element and the change will show
up, but neither ListBox.Update() nor ListBox.Refresh() change it.

I've considered a custom draw method (which I may have to implement anyway).
But is there another way of changing the text I'm overlooking? I'm not
using data binding - can a ListBox programatically bind to its own Items
collection?

Any help appreciated.

Thanks much

Jim Stanley
Media Services, Inc.

Re: ListBox component and underlying Items objects by MarkR

MarkR
Thu Jun 16 23:51:12 CDT 2005

It sounds like it's a reference issue, rather than a painting one, but
without your code I can't be sure. Check whether ListBox.AddItems() might
create a copy of your original object, and/or whether you are changing the
ElementName or just a copy of the ElementName.

(e.g. many string functions return a copy of the original string, rather
than changing it in-place; if the ElementName property accessor is returning
a copy of the ElementName, then you may simply be changing it).

Try posting a small code sample.

/m



"Jim Stanley" <Jim Stanley@discussions.microsoft.com> wrote in message
news:B00F4909-A504-4E61-8181-A6B9D39EB5BA@microsoft.com...
> Hello,
>
> I've run into a brick wall in working with a standard ListBox. I'm
> loading
> lightweight objects into a ListBox component - call them Elements. The
> Element class has an ElementName property and an overridden ToString()
> method
> that displays the ElementName. When I load Elements into my list box
> Items
> collection, the ElementName displays and all is well. My problem occurs
> when I change an ElementName property programatically. The underlying
> Element does indeed change, but I can't get the text displayed the list
> box
> to reflect it! I can delete and re-add the Element and the change will
> show
> up, but neither ListBox.Update() nor ListBox.Refresh() change it.
>
> I've considered a custom draw method (which I may have to implement
> anyway).
> But is there another way of changing the text I'm overlooking? I'm not
> using data binding - can a ListBox programatically bind to its own Items
> collection?
>
> Any help appreciated.
>
> Thanks much
>
> Jim Stanley
> Media Services, Inc.



Re: ListBox component and underlying Items objects by Morten

Morten
Fri Jun 17 04:00:20 CDT 2005

Hi Jim,

This issue has been brought up earlier, and I'm afraid I don't know any other way than to reinsert the changed item. It appears that once inserted, there is no way to tell the ListBox that it's own list of strings to be painted is out of date.


On Thu, 16 Jun 2005 23:24:03 +0200, Jim Stanley"" <Jim <Stanley@discussions.microsoft.com>> wrote:

> Hello,
>
> I've run into a brick wall in working with a standard ListBox. I'm loading
> lightweight objects into a ListBox component - call them Elements. The
> Element class has an ElementName property and an overridden ToString() method
> that displays the ElementName. When I load Elements into my list box Items
> collection, the ElementName displays and all is well. My problem occurs
> when I change an ElementName property programatically. The underlying
> Element does indeed change, but I can't get the text displayed the list box
> to reflect it! I can delete and re-add the Element and the change will show
> up, but neither ListBox.Update() nor ListBox.Refresh() change it.
>
> I've considered a custom draw method (which I may have to implement anyway).
> But is there another way of changing the text I'm overlooking? I'm not
> using data binding - can a ListBox programatically bind to its own Items
> collection?
>
> Any help appreciated.
>
> Thanks much
>
> Jim Stanley
> Media Services, Inc.
>



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