Private Sub Form_Load()
Dim i As Integer
For i = 1 To 12
ListL.AddItem "List Item " & i
Next i
SetButtonsState ' 设置按钮初始状态
End Sub
'
将左侧列表框中的指定项目移动到右侧
Private Sub ROne_Btn_Click()
MoveOneItem ListL, ListR
SetButtonsState
End Sub
'
将左侧列表框中的所有项目移动到右侧
Private Sub RAll_Btn_Click()
MoveAllItem ListL, ListR
SetButtonsState
End Sub
'
将右侧列表框中的指定项目移动到左侧
Private Sub LOne_Btn_Click()
MoveOneItem ListR, ListL
SetButtonsState
End Sub
'
将右侧列表框中的所有项目移动到左侧
Private Sub LAll_Btn_Click()
MoveAllItem ListR, ListL
SetButtonsState
End Sub
' 从列表框控件删除一条记录
Private Sub ListDelItem(ListCtrl As ListBox, ByVal delIndex As Integer)
If ListCtrl.ListCount > 0 And delIndex >= 0 Then
ListCtrl.RemoveItem delIndex
If ListCtrl.ListCount > 0 Then
If ListCtrl.ListCount <= delIndex Then
ListCtrl.ListIndex = delIndex - 1
Else
ListCtrl.ListIndex = delIndex
End If
End If
End If
End Sub
' 移动一条
Private Sub MoveOneItem(SrcList As ListBox, DesList As ListBox)
If SrcList.ListCount > 0 Then
If SrcList.ListIndex < 0 Then
SrcList.ListIndex = 0
End If
DesList.AddItem SrcList.List(SrcList.ListIndex)
DesList.ItemData(DesList.NewIndex) = SrcList.ItemData(SrcList.ListIndex)
ListDelItem SrcList, SrcList.ListIndex
End If
End Sub
' 全体移动
Private Sub MoveAllItem(SrcList As ListBox, DesList As ListBox)
Dim i As Integer
i = 0
Do While i < SrcList.ListCount
DesList.AddItem SrcList.List(i)
DesList.ItemData(DesList.NewIndex) = SrcList.ItemData(i)
i = i + 1
Loop
SrcList.Clear
End Sub
' 向源列表框中添加新项目
Private Sub AddICmd_Click()
ListL.AddItem "List Item new"
End Sub
' 从源列表框中移除项目
Private Sub RemoveICmd_Click()
If ListL.ListIndex <> -1 Then
ListL.RemoveItem ListL.ListIndex
End If
End Sub
' 清空源列表框中项目
Private Sub ClearCmd_Click()
ListL.Clear
End Sub
' 设置按钮控件是否可用
Private Sub SetButtonsState()
If ListL.ListCount > 0 Then
ROne_Btn.Enabled = True
RAll_Btn.Enabled = True
Else
ROne_Btn.Enabled = False
RAll_Btn.Enabled = False
End If
If ListR.ListCount > 0 Then
LOne_Btn.Enabled = True
LAll_Btn.Enabled = True
Else
LOne_Btn.Enabled = False
LAll_Btn.Enabled = False
End If
End Sub