VB 为什么只能全部移除

来源:百度知道 编辑:UC知道 时间:2024/05/21 10:07:55
为什么下面的代码移除时必须一次性全部移除(可以用“全部移除”或“移除选中项”)

2个List
名称分别为:List1、List2
Style都为:1
4个Command
名称分别为:CmdAdd (功能:添加选中项)
CmdAddAll (功能:全部添加)
CmdRmv (功能:移除选中项)
CmdRmvAll (功能:全部移除)
添加选中项
Private Sub CmdAdd_Click()
Dim X As Integer
While List1.SelCount > 0
For X = 0 To List1.ListCount - 1
If List1.Selected(X) = True Then
List2.AddItem List1.List(X)
List1.RemoveItem X
Exit For
End If
Next X
Wend
If List1.ListCount = 0 Then
List1.Clear
End If
End Sub
全部添加
Private Sub CmdAddAll_Click()
Dim X As Integer
While List1.ListCount > 0
For X = 0 To List1.ListCount - 1
List2.AddItem (List1.List(X))
List1.RemoveItem (X)
Exit For
Next X
Wend
List1.Clear
End Sub

移除选中项
Privat

找到原因了。
移除选中项的代码中第三、四句:
While List2.ListCount > 0
For X = 0 To List2.SelCount - 1

ListCount和SelCount位置放反了,换一下,即:

While List2.SelCount > 0
For X = 0 To List2.ListCount - 1