|
-
Jan 26th, 2009, 02:34 PM
#1
Thread Starter
Lively Member
[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.
-
Jan 26th, 2009, 02:41 PM
#2
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?
-
Jan 26th, 2009, 02:44 PM
#3
Thread Starter
Lively Member
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.
-
Jan 26th, 2009, 02:44 PM
#4
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:
Private Structure ListItem
Private displayText As String
Private itemValue As Integer
Public Sub New(ByVal text As String, ByVal value As Integer)
Me.displayText = text
Me.itemValue = value
End Sub
Public Overrides Function ToString() As String
Return displayText
End Function
Public Property Text() As String
Get
Return Me.displayText
End Get
Set(ByVal value As String)
Me.displayText = value
End Set
End Property
Public Property Value() As Integer
Get
Return Me.itemValue
End Get
Set(ByVal value As Integer)
Me.itemValue = value
End Set
End Property
End Structure
Then add instances of this structure (or class), to the listbox:
VB.NET Code:
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.
-
Jan 26th, 2009, 05:05 PM
#5
Thread Starter
Lively Member
Re: [2005] Adding item to listbox with value, and text
That worked great. Thank you.
-
Jan 26th, 2009, 06:20 PM
#6
Thread Starter
Lively Member
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.
-
Jan 26th, 2009, 06:20 PM
#7
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
-
Jan 26th, 2009, 06:58 PM
#8
Thread Starter
Lively Member
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.
-
Jan 26th, 2009, 07:00 PM
#9
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:
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.
-
Jan 26th, 2009, 07:19 PM
#10
Thread Starter
Lively Member
Re: [2005] Adding item to listbox with value, and text
Its working now thank you!
-
Mar 14th, 2009, 01:15 PM
#11
Thread Starter
Lively Member
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?
-
Mar 15th, 2009, 06:14 AM
#12
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.
-
Mar 15th, 2009, 06:45 AM
#13
Re: [2005] Adding item to listbox with value, and text
 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?
-
Mar 15th, 2009, 02:17 PM
#14
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|