Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Adding item to listbox with value, and text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Resolved [RESOLVED] [2005] Adding item to listbox with value, and text

    Hello,

    I cannot figure out how to add an item to a listbox with a specific value. I am able to add a text description without any problems, but how can I give it a specific value?

    Thanks.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] Adding item to listbox with value, and text

    What do you mean a specific value? Surely setting the text is a specific value. Can you elaborate?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    Well I would like to add an item with a .value of an integer, and a .text/description of a string, but only display the string.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Adding item to listbox with value, and text

    You can add any type of object to the ListBox, and it'll display the object by calling its ToString method. Knowing this you can create a structure (or class) like so:

    VB.NET Code:
    1. Private Structure ListItem
    2.         Private displayText As String
    3.         Private itemValue As Integer
    4.  
    5.         Public Sub New(ByVal text As String, ByVal value As Integer)
    6.             Me.displayText = text
    7.             Me.itemValue = value
    8.         End Sub
    9.  
    10.         Public Overrides Function ToString() As String
    11.             Return displayText
    12.         End Function
    13.  
    14.         Public Property Text() As String
    15.             Get
    16.                 Return Me.displayText
    17.             End Get
    18.             Set(ByVal value As String)
    19.                 Me.displayText = value
    20.             End Set
    21.         End Property
    22.  
    23.         Public Property Value() As Integer
    24.             Get
    25.                 Return Me.itemValue
    26.             End Get
    27.             Set(ByVal value As Integer)
    28.                 Me.itemValue = value
    29.             End Set
    30.         End Property
    31.  
    32.     End Structure

    Then add instances of this structure (or class), to the listbox:

    VB.NET Code:
    1. ListBox1.Items.Add(New ListItem("Hello world", 20))
    Last edited by Atheist; Jan 26th, 2009 at 07:02 PM. Reason: Included properties for the structure fields.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    That worked great. Thank you.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    Well while that worked to add the item. When I try to retreive the listbox.SelectedValue it is null. Not sure whats the issue there.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] Adding item to listbox with value, and text

    Use listbox.SelectedItem instead
    That will return an object the same type as the one you added, but if you just want the string (ie, the description that is actually displayed in the listbox) then just use listbox.SelectedItem.ToString
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    I need the value of the item though not the description/text. Selecteditem just gives me the text.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Adding item to listbox with value, and text

    SelectedItem gives you a structure object (or a class instance if you chose that route). Cast it to one and access its members:

    VB.NET Code:
    1. Dim selectedItem As ListItem = DirectCast(ListBox1.SelectedItem, ListItem)

    You'll need public properties to access the fields though. Have a look at my updated post above.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    Its working now thank you!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    I hate to revive this old thread, but is there any way access the value/member value directly from the list item without having to cast it first?

    If I gave the structure a .MemberValue function would you be able to access it directly from the list without casting it?

  12. #12
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Adding item to listbox with value, and text

    Thats not possible. The ListBox handles its items as Objects..so if you want to retrieve an item from the ListBox, it'll be returned to you as an Object. You must cast this Object to whatever type you're using in order to access members that are defined in that type.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] Adding item to listbox with value, and text

    Quote Originally Posted by yaplej
    I hate to revive this old thread, but is there any way access the value/member value directly from the list item without having to cast it first?

    If I gave the structure a .MemberValue function would you be able to access it directly from the list without casting it?
    You may be able to create your own control inheriting from the listbox, and making it return the type you want it to return. But I don't see how that will be much easier... Is it really that much work casting the item?

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: [2005] Adding item to listbox with value, and text

    I am getting use to it. Its just not as easy as I would like. They way the control works is obviously very flexible, but not intuitive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width