VB列表框编程题求助!!!
点击按钮1,将选定的项目移到List2中
点击按钮2,将List1中所有项目移到List2中
点击按钮3,将List2中选定的项目移回到List1中
点击按钮4,将List2中所有项目移回到List1中 展开
创建
两个列表框List1和List2
四个按钮Command1、Command2、Command3、Command4
分别把它们的Caption属性设置为"移动至List2"
"全部移动至List2"、"移动至List1"、"全部移动至List1"
也可以不设置,只是为了美观一点。
所有代码如下:
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
If List2.ListCount < 1 Then
List2.AddItem List1.List(i)
List1.RemoveItem i
Exit Sub
End If
For j = 0 To List2.ListCount - 1
If List1.List(i) <> List2.List(j) Then
List2.AddItem List1.List(i)
List1.RemoveItem i
Exit Sub
End If
Next j
End If
Next i
End Sub
Private Sub Command2_Click()
If List1.ListCount Then
For i = 0 To List1.ListCount - 1
List2.AddItem List1.List(i)
Next i
End If
List1.Clear
End Sub
Private Sub Command3_Click()
For i = 0 To List2.ListCount - 1
If List2.Selected(i) Then
If List1.ListCount < 1 Then
List1.AddItem List2.List(i)
List2.RemoveItem i
Exit Sub
End If
For j = 0 To List1.ListCount - 1
If List2.List(i) <> List1.List(j) Then
List1.AddItem List2.List(i)
List2.RemoveItem i
Exit Sub
End If
Next j
End If
Next i
End Sub
Private Sub Command4_Click()
If List2.ListCount Then
For i = 0 To List2.ListCount - 1
List1.AddItem List2.List(i)
Next i
End If
List2.Clear
End Sub
Private Sub Form_Load()
List1.AddItem "计算机基础"
List1.AddItem "语文"
List1.AddItem "数学"
End Sub
List2.AddItem List1.Text
List1.RemoveItem (List1.ListIndex)
End Sub
Private Sub Command2_Click()
For i = 0 To List1.ListCount - 1
List2.AddItem List1.List(i)
Next
List1.Clear
End Sub