VB:列表框和组合框的方法
PrivateSubCommand1_Click()Dimi%,j%,FoundAsBooleanIfList1.SelCount>0Then'从后往前,对选中的项查找对...
Private Sub Command1_Click()
Dim i%, j%, Found As Boolean
If List1.SelCount > 0 Then
'从后往前,对选中的项查找对方有无相同的项,若没有则移进去
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
Found = False
j = 0
Do
If List2.List(j) = List1.List(i) Then
Found = True
Else
j = j + 1
End If
Loop Until j = List2.ListCount Or Found
If Not Found Then
List2.AddItem List1.List(i)
List1.RemoveItem i
End If
End If
Next i
End If
End Sub
Private Sub Form_Load()
List1.AddItem "北京"
List1.AddItem "上海"
List1.AddItem "广州"
List1.AddItem "香港"
List1.AddItem "澳门"
List2.AddItem "北京"
List2.AddItem "上海"
List2.AddItem "天津"
List2.AddItem "重庆"
End Sub
为什么要使用For i=list1.listcount-1 to 0 step-1
这种循环方式从后往前处理呢? 展开
Dim i%, j%, Found As Boolean
If List1.SelCount > 0 Then
'从后往前,对选中的项查找对方有无相同的项,若没有则移进去
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
Found = False
j = 0
Do
If List2.List(j) = List1.List(i) Then
Found = True
Else
j = j + 1
End If
Loop Until j = List2.ListCount Or Found
If Not Found Then
List2.AddItem List1.List(i)
List1.RemoveItem i
End If
End If
Next i
End If
End Sub
Private Sub Form_Load()
List1.AddItem "北京"
List1.AddItem "上海"
List1.AddItem "广州"
List1.AddItem "香港"
List1.AddItem "澳门"
List2.AddItem "北京"
List2.AddItem "上海"
List2.AddItem "天津"
List2.AddItem "重庆"
End Sub
为什么要使用For i=list1.listcount-1 to 0 step-1
这种循环方式从后往前处理呢? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询