PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
combo box - assign value to each item-VBForums
Results 1 to 6 of 6

Thread: combo box - assign value to each item

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    combo box - assign value to each item

    Is it possible to assign a particular value to each item in a combo box?
    Or, alternatively, some similar control?

    Specifically, I want a drop down list to select from a list of countries. I then want to get the ISO 2 letter abbreviation for the selected country.

    I could have a section of code like this

    Code:
    selected_country = cmb_country.SelectedItem.ToString()
    
    if selected_country = "Afghanistan" Then
          country_code = "AF"
    ELSE IF selected_country = "Aland Islands" Then
          country_code = "AX"
    ELSE IF selected_country = "Albania" Then
          country_code = "AL"
    ELSE IF  ....
    but this will get rather tedious.

    Is there an easier way to do it? I would like to be able to assign a value to each line in the list, like

    Afghanistan, AF
    Aland Islands, AX
    Albania, AL

    then get the value for the selected item. Is this possible? If not with a combo box, then any other control?

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: combo box - assign value to each item

    basically you can use the ValueMember property to set the value.
    show us how you populate your combobox.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    Re: combo box - assign value to each item

    Quote Originally Posted by motil View Post
    basically you can use the ValueMember property to set the value.
    show us how you populate your combobox.
    The items in the combobox are coded at design time, using the items property.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: combo box - assign value to each item

    You can assign objects to the items in a combobox and it will use the ToString method of the object as the display member of the combo box. So for your example, you could create a Country class and have a few properties on it and use one of the properties as your display member. Then when an item is selected, you cast the object back to your Country class and access the properties. Like this:

    Code:
    Public Class Form1
    
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim c As New Country("United States", "US", "English")
    
            ComboBox1.Items.Add(c)
            c = New Country("France", "FR", "French")
            ComboBox1.Items.Add(c)
            c = New Country("Mexico", "MX", "Spanish")
            ComboBox1.Items.Add(c)
    
        End Sub
    
    
        Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Dim c As Country = DirectCast(ComboBox1.SelectedItem, Country)
            MessageBox.Show(c.Name + " - " + c.Abbreviation + " - " + c.DefaultLanguage)
        End Sub
    End Class
    
    Public Class Country
    
    
        Private _Name As String
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
    
    
        Private _Abbreviation As String
        Public Property Abbreviation() As String
            Get
                Return _Abbreviation
            End Get
            Set(ByVal value As String)
                _Abbreviation = value
            End Set
        End Property
    
    
        Private _DefaultLanguage As String
        Public Property DefaultLanguage() As String
            Get
                Return _DefaultLanguage
            End Get
            Set(ByVal value As String)
                _DefaultLanguage = value
            End Set
        End Property
    
    
        Public Sub New(ByVal Name As String, ByVal Abbreviation As String, ByVal Language As String)
            Me.Name = Name
            Me.Abbreviation = Abbreviation
            Me.DefaultLanguage = Language
        End Sub
    
    
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class

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

    Re: combo box - assign value to each item

    The combobox can hold any type of object, so I would just create a class that has two properties, one being the string that you want to see in the combobox and one being your associated data, in this case the ISO 2 letter abbreviation. Then you can override the ToString method of the class to make the display text be shown in the combobox. This approach would mean that you need to populate the items by code rather than in the designer though so you would have to just create and add the items in your form Load event handler for them to be there as soon as the form is opened.

    Here's an example of how your class could look:
    vb Code:
    1. Public Class CountryItem
    2.  
    3.     Private _CountryName As String
    4.     Public Property CountryName() As String
    5.         Get
    6.             Return _CountryName
    7.         End Get
    8.         Set(ByVal value As String)
    9.             _CountryName = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private _IsoCode As String
    14.     Public Property IsoCode() As String
    15.         Get
    16.             Return _IsoCode
    17.         End Get
    18.         Set(ByVal value As String)
    19.             _IsoCode = value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Overrides Function ToString() As String
    24.         Return MyBase.ToString()
    25.     End Function
    26.  
    27.     Public Sub New(ByVal Name As String, ByVal Code As String)
    28.         _IsoCode = Code
    29.         _CountryName = Name
    30.     End Sub
    31.  
    32. End Class

    and then to add these items to the combobox:

    vb Code:
    1. Combobox1.Items.Add(New CountryItem("Afghanistan", "AF"))
    2. Combobox1.Items.Add(New CountryItem("Albania", "AL"))
    3. 'etc etc

    Now to get at the ISO code for the selected item you need to cast the item to the correct type (our CountryItem class) by using DirectCast like so:
    vb Code:
    1. MessageBox.Show(DirectCast(Combobox1.SelectedItem, CountryItem).IsoCode)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


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

    Re: combo box - assign value to each item

    Damn! Beaten to it
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width