Option Explicit
'设计阶段先给ComboBox的属性Sorted设置为True值
'测试后,一些不使用的功能,自行删除代码
Dim i As Integer, Lstr As String
Private Sub Combo1_Click()
'点击组合框后,让列表框取得该数据的驻点
For i = 0 To List1.ListCount - 1
If List1.List(i) = Combo1.Text Then Exit For
Next i
List1.Selected(i) = True
End Sub
Private Sub Command1_Click()
'添加
'如果数据为空,不添加
If Trim(Combo1.Text) = "" Then
MsgBox "请先填入数据"
Exit Sub
End If
'检查是否重复添加
For i = 0 To List1.ListCount - 1
If List1.List(i) = Combo1.Text Then
MsgBox "该数据已经存在,不予添加。"
Exit Sub
End If
Next i
'添加
List1.AddItem Combo1.Text
Combo1.AddItem Combo1.Text
Combo1.Refresh: List1.Refresh
End Sub
Private Sub Command2_Click()
If List1.ListIndex < 0 Then
MsgBox "请先点击要删除的项目。"
Exit Sub
End If
'记录将被删除的值
Lstr = List1.List(List1.ListIndex)
'删除被点击的List
List1.RemoveItem List1.ListIndex
'删除Combo1对应值
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = Lstr Then Exit For
Next i
Combo1.RemoveItem i
End Sub
Private Sub Command3_Click()
'清除列表框
List1.Clear
'清除组合框
Combo1.Clear
End Sub
Private Sub Form_Load()
Combo1.Clear
List1.Clear
'先添加5个测试用途的数据
For i = 5 To 1 Step -1
Combo1.AddItem i & Chr(i + 64)
List1.AddItem i & Chr(i + 64)
Next i
End Sub
Private Sub List1_Click()
Combo1.Text = List1.List(List1.ListIndex)
End Sub