How could I expand the Listbox Control

  • Thread starter Thread starter pnp
  • Start date Start date
Hi Peter,

The listbox is the server representation of a HTML SELECT , that's why you
can only store string , at least that is what I think.

to overcome this you can do several things, maybe not very efficient or
elegants but that solve your problem :)
keep the objects in an ArrayList or any other collection object and then in
the List box you set as the Text what you want and the Value will be either
the index of that element in the arraylist or an ID of that object if the
collection used does not support indexing .
then in the postback you can get this value and use it to get it back the
selected object from the collection.

Hint:
You may use SelectedIndex as a shortcut if you are using the index and not
including an extra value in the select.


Cheers,
 
Hi pnp,

I read Ignacio's answer on this. It seems that you hould be more clear what
thype of list box you are talking about. Is it WindowsForms list box ot
WebControl. Because with windows forms list box you can store what ever type
of object you want. What you see in the list box is what object's ToString()
method returns
 
I was talking about a winforms listbox. The problem with overriding the
ToString() method is that if I want to change the text of that object, the
listbox control doesn't refresh it's contents, but still continuous to show
the previous value.

Stoitcho Goutsev (100) said:
Hi pnp,

I read Ignacio's answer on this. It seems that you hould be more clear what
thype of list box you are talking about. Is it WindowsForms list box ot
WebControl. Because with windows forms list box you can store what ever type
of object you want. What you see in the list box is what object's ToString()
method returns

--

Stoitcho Goutsev (100) [C# MVP]


pnp said:
to store other items that just strings?

Thanks
Peter
 
pnp,

Yes, that is because you don't actually change the list box. we need to
remove and add the item in order listbox to update. Don't forget that .NET
listbox controls uses internally Windows listbox, which in turn can load
only strings. So even if you call refresh, underlaying native control won't
reload the strings and will show the old string.

If you know the index of the item you can use the following dummy looking
line

this.listBox1.Items[index] = this.listBox1.Items[index];

--
HTH
Stoitcho Goutsev (100) [C# MVP]


pnp said:
I was talking about a winforms listbox. The problem with overriding the
ToString() method is that if I want to change the text of that object, the
listbox control doesn't refresh it's contents, but still continuous to show
the previous value.

Stoitcho Goutsev (100) said:
Hi pnp,

I read Ignacio's answer on this. It seems that you hould be more clear what
thype of list box you are talking about. Is it WindowsForms list box ot
WebControl. Because with windows forms list box you can store what ever type
of object you want. What you see in the list box is what object's ToString()
method returns

--

Stoitcho Goutsev (100) [C# MVP]


pnp said:
to store other items that just strings?

Thanks
Peter
 

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

Back
Top