Manipulating combo boxes in Vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thiago777
    New Member
    • May 2007
    • 89

    Manipulating combo boxes in Vb.net

    Hi,
    I found a way to include my items dynamically by following this how-to:
    http://www.mgbrown.com/PermaLink37.asp x

    If you dont want to take a look on the link here it goes:

    Code:
    Public Class ValueDescriptionPair
       '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       'Class used to control the comboBox components
       '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
       'Usage:
       'To add items to the list you use code like this:
       '  
       'ComboBox1.Items.Add(New ValueDescriptionPair(1, "first item"))
       'ComboBox1.Items.Add(New ValueDescriptionPair(2, "second item"))
       'ComboBox1.Items.Add(New ValueDescriptionPair(3, "third item"))
       '
       'To get the value stored in Value and displays in a message box:
       'Dim ItemSelected As Integer
       'ItemSelected = CType(ComboBox1.SelectedItem,ValueDescriptionPair).Value
       'MsgBox("Value Selected = " & ItemSelected)
       '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
       Private m_Value As Object
       Private m_Description As String
    
    #Region "properties"
    
       Public ReadOnly Property Value() As Object
          Get
             Return m_Value
          End Get
       End Property
       Public ReadOnly Property Description() As String
          Get
             Return m_Description
          End Get
       End Property
    
    #End Region
    
       Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String)
          m_Value = NewValue
          m_Description = NewDescription
       End Sub
    
       Public Overrides Function ToString() As String
          Return m_Description
       End Function
    
    End Class
    But, what if you want to change the selected property to an ValueDescriptio nPair added value?

    For example, I did:

    Code:
    CmbBoxType.Items.Add(New ValueDescriptionPair(1, "Type 1")) 
    CmbBoxType.Items.Add(New ValueDescriptionPair(2, "Type 2"))
    on form load.. and then to set the selected value to the second option the .SelectedItem property receives what?
    How would you know the reference to your ValueDescriptio nPair like this?

    thanks
  • thiago777
    New Member
    • May 2007
    • 89

    #2
    Just found a solution, dont know if the best but I simply store a reference of all ValueDescriptio nPair in an array. Like this:

    Private TypesArray(Type sAvailable - 1) As ValueDescriptio nPair

    Code:
    TypesArray(0) = New ValueDescriptionPair(1, "Type 1")
    TypesArray(1) = New ValueDescriptionPair(2, "Type 2")
    For i As Integer = 0 To TypesArray.Length - 1
             CmbBoxType.Items.Add(TypesArray(i))
    Next
    then I can say:

    Code:
    CmbBoxType.SelectedItem = ConnTypesArray(0)
    hope this helps someone with similar problem

    Comment

    Working...