Listbox Item Text vs. Value 
Author Message
 Listbox Item Text vs. Value

Hi,

This is a newbie question, but I could not find a way to
add text-value pairs to listboxes or comboboxes. I am more
adept at web development, which allows listboxes to have a
text value that appears on the list, but also an
underlying value field for each item as well. I got around
this with a parallel array, but I'd like to know if this
is possible with .NET listboxes.

Thank you,

Ram



Sun, 06 Nov 2005 05:44:23 GMT  
 Listbox Item Text vs. Value

Quote:
> This is a newbie question, but I could not find a way to
> add text-value pairs to listboxes or comboboxes. I am more
> adept at web development, which allows listboxes to have a
> text value that appears on the list, but also an
> underlying value field for each item as well. I got around
> this with a parallel array, but I'd like to know if this
> is possible with .NET listboxes.

The items in a combobox/listbox can be of any type, so each item can have a
text, a value and thousands of other properties. Example:

public class MyItem
    public Text as string
    public Value as integer

    public sub new(Text as string,Value as integer)
        me.text=text
        me.value=value
    end sub
    public overrides function ToString as string
        return text
    end function
end class
'...

fill listbox:
listbox1.items.add(new myitem("item1",1"))
listbox1.items.add(new myitem("item2",2"))

The value returned by the ToString function is the one displayed in the
listbox.

--
Armin



Sun, 06 Nov 2005 06:14:13 GMT  
 Listbox Item Text vs. Value

Quote:
> This is a newbie question, but I could not find a way to
> add text-value pairs to listboxes or comboboxes. I am more
> adept at web development, which allows listboxes to have a
> text value that appears on the list, but also an
> underlying value field for each item as well. I got around
> this with a parallel array, but I'd like to know if this
> is possible with .NET listboxes.

Hi, you can use my little ItemData class that I posted here:

    http://tinyurl.com/c932

You can modify it according to your needs.

-- Alan



Sun, 06 Nov 2005 06:21:20 GMT  
 Listbox Item Text vs. Value
Thank you Alan and Armin. Both of your similar solutions
will work. I need an Item Data class. I was hoping that
the text/value pair was built into the listbox class, but
as I suspected, it's not.

Ram

Quote:
>-----Original Message-----
>Hi,

>This is a newbie question, but I could not find a way to
>add text-value pairs to listboxes or comboboxes. I am
more
>adept at web development, which allows listboxes to have
a
>text value that appears on the list, but also an
>underlying value field for each item as well. I got
around
>this with a parallel array, but I'd like to know if this
>is possible with .NET listboxes.

>Thank you,

>Ram

>.



Sun, 06 Nov 2005 06:37:50 GMT  
 Listbox Item Text vs. Value
Hello,


Quote:
> This is a newbie question, but I could not find a way to
> add text-value pairs to listboxes or comboboxes. I am more
> adept at web development, which allows listboxes to have a
> text value that appears on the list, but also an
> underlying value field for each item as well. I got around
> this with a parallel array, but I'd like to know if this
> is possible with .NET listboxes.

Create a structure and add a ToString method which returns the Caption:

\\\
Dim p As Person = New Person()
p.Name = "Pink Panther"
p.Age = 22

Me.ComboBox1.Items.Add(p)

MessageBox.Show(DirectCast(Me.ComboBox1.Items.Item(0), Person).ToString())
.
.
.

Public Class Person
    Private m_strName As String
    Private m_ingAge As Integer

    Public Property Name() As String
        Get
            Return m_strName
        End Get
        Set(ByVal Value As String)
            m_strName = Value
        End Set
    End Property

    Public Property Age() As Integer
        Get
            Return m_intAge
        End Get
        Set(ByVal Value As Integer)
            m_intAge = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return m_strName & " (" & m_intAge.ToString() & ")"
    End Function
End Class
///

Regards,
Herfried K. Wagner
--
http://www.mvps.org/dotnet



Sun, 06 Nov 2005 06:56:16 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Capturing item values from multiselect listbox

2. Selected Listbox Item using ItemData value???

3. Different Text Color for each item in a ListBox

4. ListBox - get item text

5. Changing the text color of a listbox item

6. Write contents of listbox to text file/populate listbox from text file

7. Listbox.Text to integer value

8. Save Text Boxes Values To Combo Box's Item

9. Usering script to display the contents(text,value) of the option of a html listbox

10. Populating listbox with values from a text file

11. Move ListBox items to another ListBox

12. Listbox with single character item corrupts item?

 

 
Powered by phpBB® Forum Software