| ComboBox and ListBox handling is fairly complex. The code in this form demonstrates how to insert and extract data. It also shows you how to avoid inserting already-existing data a second time. Option Explicit Private Sub Form_Load() cmdLoad_Click End Sub Private Sub cmdLoad_Click() Dim c As Long Dim ItemText As String cmdClear_Click For c = 1 To 10 ItemText = "Item" & c lstDataChk.AddItem ItemText Next End Sub Private Sub cmdClear_Click() lstDataChk.Clear lstData.Clear cboDropdown.Clear cboSimple.Clear cboDropdownList.Clear End Sub Private Sub lstDataChk_ItemCheck (Item As Integer) Dim c As Long Select Case lstDataChk.Selected(Item) Case True 'if checked Dim CheckFlag As Boolean CheckFlag = False For c = 0 To lstData.ListCount 'if not in lstData If lstData.List(c) = _ lstDataChk.List(Item) Then CheckFlag = True End If Next If Not CheckFlag Then lstData.AddItem lstDataChk.List(Item) End If Case False 'if unchecked: For c = 0 To lstData.ListCount 'if in lstData: If lstData.List(c) = _ lstDataChk.List(Item) Then lstData.RemoveItem (c) End If Next End Select lblCheckedCount = "Checked items: (" & _ Trim(lstDataChk.SelCount) & ")" End Sub Private Sub lstData_DblClick() cboDropdown.AddItem lstData.List(lstData.ListIndex) cboSimple.AddItem lstData.List(lstData.ListIndex) cboDropdownList.AddItem _ lstData.List(lstData.ListIndex) End Sub Private Sub cboDropdown_Click() txtComboClick = cboDropdown.Text txtComboText = "" End Sub Private Sub cboDropdown_Change() txtComboText = cboDropdown.Text txtComboClick = "" End Sub Private Sub cboSimple_Click() txtComboClick = cboSimple.Text txtComboText = "" End Sub Private Sub cboSimple_Change() txtComboText = cboSimple.Text txtComboClick = "" End Sub Private Sub cboDropdownList_Click() txtComboClick = cboDropdownList.Text txtComboText = "" End Sub Private Sub cboDropdownList_Change() txtComboText = cboDropdownList.Text txtComboClick = "" End Sub |
Handle Data for ComboBox and ListBox |
Express News India | Freelance ecommerce web development India