Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.Clear()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 20
ComboBox1.Items.Add("Item " + intLoopIndex.ToString())
Next
ComboBox1.Text = "Select one..."
MsgBox("The combo box contains " & ComboBox1.Items.Count & " items.")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox1.SelectedIndex > -1 Then
Dim selectedIndex As Integer
selectedIndex = ComboBox1.SelectedIndex
Dim selectedItem As Object
selectedItem = ComboBox1.SelectedItem
TextBox1.Text = "Selected item text: " & selectedItem.ToString() & _
" Selected index: " & selectedIndex.ToString()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ComboBox1.Sorted = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ComboBox1.Items.Clear()
Dim Objects(20) As DataItem
Dim intLoopIndex As Integer
For intLoopIndex = 0 To 20
Objects(intLoopIndex) = New DataItem("Item " & intLoopIndex, _
CSng(intLoopIndex))
' ComboBox1.Items.Add(Objects(intLoopIndex))
Next
ComboBox1.Items.AddRange(Objects)
ComboBox1.Text = "Select one..."
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex > -1 Then
If (ComboBox1.SelectedItem.GetType Is GetType(DataItem)) Then
TextBox1.Text = "The data for the item you selected is: " & _
CType(ComboBox1.SelectedItem, DataItem).GetData()
End If
End If
End Sub
Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
TextBox1.Text = ComboBox1.Text
End Sub
End Class
Public Class DataItem
Private Data As Single
Private Name As String
Public Sub New(ByVal NameArgument As String, ByVal Value As Single)
Name = NameArgument
Data = Value
End Sub
Overrides Function ToString() As String
Return CStr(Name)
End Function
Public Function GetData() As Single
Return Data
End Function
End Class