使两个列表框(ListBox)的选项同步
1. 在 Form 中添加两个 ListBox 和一个 CommandButton 一个 Timer, 不要改动他们的属性。
2. 在 Form 中添加以下代码:
Private Sub Form_Load()
Dim X As Integer
For X = 1 To 26
List1.AddItem Chr$(X + 64)
Next X
For X = 1 To 26
List2.AddItem Chr$(X + 64)
Next X
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub Command1_Click()
End
End Sub
Private Sub timer1_Timer()
Static PrevList1
Dim TopIndex_List1 As Integer
TopIndex_List1 = List1.TopIndex
If TopIndex_List1 <> PrevList1 Then
List2.TopIndex = TopIndex_List1
PrevList1 = TopIndex_List1
End If
If List1.ListIndex <> List2.ListIndex Then
List2.ListIndex = List1.ListIndex
End If
End Sub
运行程序,当选中其中一个列表框中的某一项后,另外一个列表框中的相应项就会被选中。