Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add a value to listbox item

Posted on 2006-05-20
7
Medium Priority
?
293 Views
Last Modified: 2012-08-13
I am trying to add a value to a listbox item and currently I can only add the display text. Is there a way to add a new ListItem as you can in ASP.Net? I know I can use the selected index, but that won't work for my purposes. I need to have the value equal to the index of another list. Any help would be great! Thanks! (current code below)


CompanyList.Items.Add(Name & "  - " & Org)
0
Comment
Question by:strickdd
  • 4
  • 3
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 16725356
Listbox item can be any type, not only string. You can define class which contains text and any additional data, and add instances of this class to listbox. Class must have ToString function which is used by Listbox to display item.
0
 
LVL 28

Author Comment

by:strickdd
ID: 16725538
So something like (syntax might be off a little):

Public Class ListItem

dim _value as Integer
dim _text as string

Public Property Text
     Get
           return _text
     End

     Set
          _text = value
     End
End Property

Public Property Value
     Get
           return _value
     End

     Set
          _value= value
     End
End Property

Public Function ToString() As String
    return _text
End Function

End Class
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16726869
Yes, I think you need to add Overrides to ToString function. You can add such items to Listbox directly, since Listbox.Items.Add function accepts Object type. To read item from listbox you need to cast it to ListItem.
I think it is convenient to add Listitem constructor which accepts (String, Integer). Text and Value peoperties can be read-only if you don't need to change them dynamically.

Using:

Listbox1.Items.Add(new ListItem("text", 1))
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Author Comment

by:strickdd
ID: 16726900
So rather than have two seperate properties in the class, I should just make the constructor accept the string and the integer... Also, how do i add the overrides to the ToString function?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16727079
Public Overrides Function ToString() As String

This means, this function overrides Overridable Object.ToString function. This is important because only in this case right implementation of ToString will be called.

>> I should just make the constructor accept the string and the integer

You can do this if you want. Without such constructor you must write:

Dim listItem as ListItem = new ListItem()
listItem.Text = "text"
listItem.Value = 1
Listbox1.Items.Add(listItem)

Having such constructor you can write:

Listbox1.Items.Add(new ListItem("text", 1))

However, when you read Listitem object from listbox, you need to access it's members, so you still need properties. This is the reason why I suggest to make them read-only (Get).
0
 
LVL 28

Author Comment

by:strickdd
ID: 16729433
Thanks for all the help. I"m sure this will work, but I will have to test it tonight.
0
 
LVL 28

Author Comment

by:strickdd
ID: 16733795
For everyone who reads this, here is the final, syntactically correct version:

Public Class ListItem

    Private _text As String
    Private _value As Integer

    Public Sub New(ByVal Text As String, ByVal Value As Integer)
        _text = Text
        _value = Value
    End Sub

    Public ReadOnly Property Text()
        Get
            Return _text
        End Get
    End Property

    Public ReadOnly Property Value()
        Get
            Return _value
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return _text
    End Function

End Class
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question