Vb.net 如何删除listview中的项目?运动模拟

Vb.net 如何删除listview中的项目?运动模拟,vb.net,Vb.net,我在listview中填充项目,当我单击命令按钮时,我会将项目添加到数据库的列表中,如果列表中的项目尚未签出,它会从下面填充的列表中删除项目,即我的代码 Dim y As Integer Dim a As String y = ListView2.Items.Count While y >= 0 a = ListView2.Items.Item(y).Text y = y - 1 Dim TMP_SQL_VAL As String = "select count(

我在
listview
中填充项目,当我单击命令按钮时,我会将项目添加到数据库的列表中,如果列表中的项目尚未签出,它会从下面填充的列表中删除项目,即我的代码

Dim y As Integer
Dim a As String
y = ListView2.Items.Count
While y >= 0
    a = ListView2.Items.Item(y).Text
    y = y - 1

    Dim TMP_SQL_VAL As String = "select count([Check-Out]) from tbl_list1 where barcode = '" + a + "'"
    locconn.Open()
    command = New SqlCommand(TMP_SQL_VAL, locconn)
    Dim READER As SqlDataReader
    READER = command.ExecuteReader()
    READER.Read()

    If READER(0) = 0 Then
        MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation)
        clear_text()

        ListView2.Items.Remove(y)
    Else
        READER.Close()
        Dim READER2 As SqlDataReader
        Dim TMP_SQL_VAL2 = "select [Check-In] from tbl_list1 where barcode = '" + a + "' and  [User] = '" + rsuser + "'"
        Dim cmd = New SqlCommand(TMP_SQL_VAL2, locconn)
        READER2 = cmd.ExecuteReader
        READER2.Read()
        If READER2(0) Is Nothing Then
            MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation)
            clear_text()
        ListView2.Items.Remove(y)
        End If
   end if
   locconn.Close()
End While

Catch ex As Exception
    MsgBox(ex.Message)
    localconn.ShowDialog()
Finally
    locconn.Close()
End Try

尝试检查y的值是否<0,如果它是空的,则处理终止它

Dim y As Integer
Dim a As String
y = ListView2.Items.Count - 1 'Reduce 1
If y < 0 Then return 'If no item then terminate
While y >= 0
    a = ListView2.Items.Item(y).Text
    'y = y - 1    'Moved to last line before End While
    Dim TMP_SQL_VAL As String = "select count([Check-Out]) from tbl_list1 where barcode = '" + a + "'"
    locconn.Open()
    command = New SqlCommand(TMP_SQL_VAL, locconn)
    Dim READER As SqlDataReader
    READER = command.ExecuteReader()
    READER.Read()

    If READER(0) = 0 Then
        MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation)
        clear_text()

        ListView2.Items.Remove(y)
    Else
        READER.Close()
        Dim READER2 As SqlDataReader
        Dim TMP_SQL_VAL2 = "select [Check-In] from tbl_list1 where barcode = '" + a + "' and  [User] = '" + rsuser + "'"
        Dim cmd = New SqlCommand(TMP_SQL_VAL2, locconn)
        READER2 = cmd.ExecuteReader
        READER2.Read()
        If READER2(0) Is Nothing Then
            MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation)
            clear_text()
        ListView2.Items.Remove(y)
        End If
   end if
   locconn.Close()

   y -= 1  'Move here so the value is decremented before the While statement

End While

Catch ex As Exception
    MsgBox(ex.Message)
    localconn.ShowDialog()
Finally
    locconn.Close()
End Try
Dim y作为整数
像线一样变暗
y=ListView2.Items.Count-1'减少1
如果y<0,则返回“如果没有项目,则终止”
当y>=0时
a=ListView2.Items.Item(y).Text
“y=y-1”移动到结束前的最后一行,而
Dim TMP_SQL_VAL As String=“从tbl_列表1中选择计数([Check Out]),其中条形码='“+a+””
locconn.Open()
命令=新的SqlCommand(TMP_SQL_VAL,locconn)
作为SqlDataReader的Dim读取器
READER=command.ExecuteReader()
READER.Read()
如果读卡器(0)=0,则
MsgBox(“条形码:&a&”仍在里面”,MsgBoxStyle.叹号)
清除文本()
ListView2.Items.Remove(y)
其他的
READER.Close()
Dim读取器2作为SqlDataReader
Dim TMP_SQL_VAL2=“从tbl_列表1中选择[Check In],其中条形码=”“+a+”,[User]=”“+rsuser+””
Dim cmd=新的SqlCommand(TMP_SQL_VAL2,locconn)
READER2=cmd.ExecuteReader
READER2.Read()
如果READER2(0)为空,则
MsgBox(“条形码:&a&”仍在里面”,MsgBoxStyle.叹号)
清除文本()
ListView2.Items.Remove(y)
如果结束
如果结束
locconn.Close()
y-=1'移动到此处,使值在While语句之前递减
结束时
特例
MsgBox(例如消息)
localconn.ShowDialog()
最后
locconn.Close()
结束尝试

使用此链接……在这种情况下,您应该使用RemoveAt而不是Remove,因为您使用的是y作为索引。RemoveAt删除指定索引处的项。Remove移除项目本身,您需要为每个项目使用某种类型的方法来循环listview中的所有项目,以获取ListViewItem本身。我只是想帮你,我想这是可以回答的。但是我不断得到错误“InvalidArguments=value of'-1'对'index'无效”。参数名index“你必须在y上减去1,比如:y=Listview1.Items.Count-1因为vb.net使用零基索引,很抱歉,我无法编辑我以前的评论。