# listbox同步

Posted by Thomasaria on July 21, 1998 at 11:28:49:

(sorry占用本版，刚mail给该网页作者但被退回，所以公布于此看看大家有否意见)

在VB心得笔记网页(http://sunh.hosp.ncku.edu.tw/~cww/)中有篇「使两个ListBox项目同步」(http://sunh.hosp.ncku.edu.tw/~cww/htm28.htm)，写的指令多了些，

Option Explicit

Sub Form_Load()
'Initialize two list boxes with the alphabet
Dim i As Long
For i = 1 To 26
List1.AddItem Chr$(i + 64)
Next i
For i = 1 To 26
List2.AddItem Chr$(i + 64)
Next i
Timer1.Interval = 1
Timer1.Enabled = True
End Sub

Sub Timer1_Timer()
Dim TopIndex_List1 As Long
Static PrevTI_List1
'Get the index for the first item in the visible list
TopIndex_List1 = List1.TopIndex
'See if the top index has changed
If TopIndex_List1 <> PrevTI_List1 Then
'Set the top index of List2 equal to List1 so that the list boxes
'scroll to the same relative position
List2.TopIndex = TopIndex_List1
'Keep track of the current top index
PrevTI_List1 = TopIndex_List1
End If
'Select the item in the same relative position in both list boxes
If List1.ListIndex <> List2.ListIndex Then
List2.ListIndex = List1.ListIndex
End If
End Sub

可是实际上我简单用如下即可在拉卷轴时使两个ListBox项目同步

Private Sub List1_Scroll(Index As Integer)
'按卷动轴时list项目位置同步
List2.TopIndex = List1.TopIndex
End Sub

Private Sub List2_Scroll(Index As Integer)
'按卷动轴时list项目位置同步
List1.TopIndex = List2.TopIndex
End Sub

用计时器有何特殊理由吗？

Thomasaria

Follow Ups:

- 你的做法是对的！ 小吴 07:52:23 7/22/98 (3)
  - Re: 你的做法是对的！ cww 15:25:44 7/23/98 (0)
  - To:CWW Albert 09:45:03 7/23/98 (1)
    - 您是对的 cww 15:43:38 7/23/98 (0)