VB6.0中删除listbox选中内容的问题

这是一个用commandbutton来实现listbox中内容的删除代码Private Sub Command1_Click()Dim a As IntegerWhile List1.SelCount > 0For a = 0 To List1.ListCount - 1If List1.Selected(a) = True ThenList1.RemoveItem List1.ListIndexEnd IfNext aExit ForWendEnd Sub大家快帮我看一下哪里有错误,一运行就卡住了.....

因为删除的时候改变了List1.ListCount.修改一下For语句即可:
For a = List1.ListCount - 1 To 0 Step -1
另外也不需要Exit For,我帮你改了下代码:
Option Explicit
Private Sub Command1_Click()
Dim a As Integer
If List1.SelCount > 0 Then
For a = List1.ListCount - 1 To 0 Step -1
If List1.Selected(a) Then List1.RemoveItem a
Next
End if
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-27
你是不是想删除选中的项目,可以参考一下我的代码:
Private Sub Command1_Click()
Dim i As Integer
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
List1.RemoveItem i
End If
Next
End Sub
第2个回答  2022-01-20
If ListBox1.ListCount >= 1 Then
If ListBox1.Selected(ListBox1.ListIndex) Then ListBox1.RemoveItem ListBox1.ListIndex
End If
listbox1 为列表框名称
ListBox1.ListIndex 为列表框所选值的行号
ListBox1.Selected() 判断所选的值是否为真(True)
ListBox1.RemoveItem 删除所选的行
第3个回答  2011-07-27
Private Sub Command1_Click()
If List1.SelCount > 0 Then
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then List1.RemoveItem i
Next
End If
End Sub
第4个回答  2011-07-27
Private Sub Command1_Click()
Dim a As Integer
While List1.SelCount > 0
For a = 0 To List1.ListCount - 1
If List1.Selected(a) = True Then
List1.RemoveItem
List1.ListIndex
End If
Next a
Exit ForWend
End Sub
相似回答