Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am assuming that you are wanting to add items to a ComboBox on an Windows form. Although Klaus is on the right track I believe that the ListItem class is a member of the System.Web.UI.WebControls namespace. So you shouldn't be using it in a Windows forms solution. You can, however, create your own class that you can use in its place. Create a simple class called MyListItem (or whatever name you choose) like this:</p> <pre><code>Public Class MyListItem Private mText As String Private mValue As String Public Sub New(ByVal pText As String, ByVal pValue As String) mText = pText mValue = pValue End Sub Public ReadOnly Property Text() As String Get Return mText End Get End Property Public ReadOnly Property Value() As String Get Return mValue End Get End Property Public Overrides Function ToString() As String Return mText End Function End Class </code></pre> <p>Now when you want to add the items to your ComboBox you can do it like this:</p> <pre><code>myComboBox.Items.Add(New MyListItem("Text to be displayed", "value of the item")) </code></pre> <p>Now when you want to retrieve the value of the selected item from your ComboBox you can do it like this:</p> <pre><code>Dim oItem As MyListItem = CType(myComboBox.SelectedItem, MyListItem) MessageBox.Show("The Value of the Item selected is: " &amp; oItem.Value) </code></pre> <p>One of the keys here is overriding the ToString method in the class. This is where the ComboBox gets the text that is displayed.</p> <hr> <p>Matt made an excellent point, in his comment below, about using Generics to make this even more flexible. So I wondered what that would look like.</p> <p>Here's the new and improved <code>GenericListItem</code> class:</p> <pre><code>Public Class GenericListItem(Of T) Private mText As String Private mValue As T Public Sub New(ByVal pText As String, ByVal pValue As T) mText = pText mValue = pValue End Sub Public ReadOnly Property Text() As String Get Return mText End Get End Property Public ReadOnly Property Value() As T Get Return mValue End Get End Property Public Overrides Function ToString() As String Return mText End Function End Class </code></pre> <p>And here is how you would now add Generic items to your ComboBox. In this case an Integer:</p> <pre><code>Me.myComboBox.Items.Add(New GenericListItem(Of Integer)("Text to be displayed", 1)) </code></pre> <p>And now the retrieval of the item:</p> <pre><code>Dim oItem As GenericListItem(Of Integer) = CType(Me.myComboBox.SelectedItem, GenericListItem(Of Integer)) MessageBox.Show("The value of the Item selected is: " &amp; oItem.Value.ToString()) </code></pre> <p>Keep in mind that the type <code>Integer</code> can be any type of object or value type. If you want it to be an object from one of your own custom classes that's fine. Basically anything goes with this approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload