Home All Groups Group Topic Archive Search About

Drop down listbox - extra properties, possible?

Author
1 Jun 2005 6:37 PM
postings
Hi

I am constantly dealing with situations whereby it would be nice to
have, say, a couple more values embedded in a listbox, that would be
nice and easy to retrieve in code.

i.e. like this:

----------------------------------------------------

Dim li = New ListItem
li.text = "Pounds Sterling"
li.value = "1"
li.value2 = "100" 'Note Value2 is not a real property
ddlTest.Items.Add(li)
li.text = "Dollars"
li.value = "2"
li.value2 = "135" 'Note Value2 is not a real property
ddlTest.Items.Add(li)
End Sub

----------------------------------------------------

So when the user select the listbox, not only can I access
ddlTest.SelectedItem and ddlTest.Selectedvalue, but I could also have
ddlTest.Selectedvalue2

I'm always having to write code around listboxes, and generally I end
up putting a few overflow values in a viewstate array, which can get
very messy having to iterate through this sort of code.

I am wondering, how would you go about adding an extra value property
to the base class of a listbox, be able to retrieve this value, and
still be able to drag a ddlistbox onto the form in VS2003.

How simple would this be to achieve?
Has anybody achieved it?
Any examples or comments?

Cheers

Alex

Author
1 Jun 2005 9:15 PM
Visar Gashi, MCP
Hello,

You could try and extend the ListItem class and add your extra properties.

Say you create a class:

class ListItem2 : ListItem
{
...
}

Then when you use it:

ListItem2 li2 = new ListItem2();

....

ddlTest.Items.Add((ListItem)li2);

When you retrieve it:
ListItem2 li2_copy = (ListItem2)ddlTest.Items[0];

I have not tested this code, but you should get the idea from it.

Regards,
-Visar

Show quoteHide quote
"posti***@alexshirley.com" wrote:

> Hi
>
> I am constantly dealing with situations whereby it would be nice to
> have, say, a couple more values embedded in a listbox, that would be
> nice and easy to retrieve in code.
>
> i.e. like this:
>
> ----------------------------------------------------
>
> Dim li = New ListItem
> li.text = "Pounds Sterling"
> li.value = "1"
> li.value2 = "100" 'Note Value2 is not a real property
> ddlTest.Items.Add(li)
> li.text = "Dollars"
> li.value = "2"
> li.value2 = "135" 'Note Value2 is not a real property
> ddlTest.Items.Add(li)
> End Sub
>
> ----------------------------------------------------
>
> So when the user select the listbox, not only can I access
> ddlTest.SelectedItem and ddlTest.Selectedvalue, but I could also have
> ddlTest.Selectedvalue2
>
> I'm always having to write code around listboxes, and generally I end
> up putting a few overflow values in a viewstate array, which can get
> very messy having to iterate through this sort of code.
>
> I am wondering, how would you go about adding an extra value property
> to the base class of a listbox, be able to retrieve this value, and
> still be able to drag a ddlistbox onto the form in VS2003.
>
> How simple would this be to achieve?
> Has anybody achieved it?
> Any examples or comments?
>
> Cheers
>
> Alex
>
>

Bookmark and Share