关于VB的移除

来源:百度知道 编辑:UC知道 时间:2024/05/15 15:09:32
例如窗体上有个列表框,一个按钮。multiselect设置为2
列表框中有6个项目, 怎么才能实现 移除选中的项目.

我自己编的,可是会报错:
private sub command1_click()
for i=0 to 5
if List1.Selected(i) then list2.removeitem selected(i)
next i
end sub
private sub command1_click()
for i=0 to 5
if List1.Selected(i) then list1.removeitem list1.selected(i)
next i
end sub

Augusboy:我的意思是选中多项 然后移除。 还有 如果移除全部 还不如直接用 list1.clear

哦,那么应该是
Private Sub Command1_Click()
Dim f As Long
While (List1.SelCount > 0)
If List1.Selected(f) = True Then
List1.RemoveItem (f)
Else
f = f + 1
End If
Wend
End Sub

因为用了removeitem以后index的值会减少1,所以只要从最后一项开始倒过来移除就没问题了

private sub command1_click()
for i= 5 to 0 step -1
if List1.Selected(i)= true then list1.removeitem(i)
next i
end sub