如何使用vbyesno删除列表视图中的选定项目

时间:2018-05-21 13:51:11

标签: vba access-vba

此代码正在删除listview中的所选项目,除非出于某种原因显示response 2次并删除数据两次。它应该只出现一次以确认我是否要删除我选择的项目。

   Dim response As Integer

    For Each i As ListViewItem In ListView.SelectedItems
        response = MsgBox("Are you sure you want to delete " + TextBox.Text, vbYesNo, "Confirm Delete")
        If response = vbYes Then
            ListView.Items.Remove(i)
        End If
    Next

2 个答案:

答案 0 :(得分:0)

您需要退出for循环

Dim response As Integer

For Each i As ListViewItem In ListView.SelectedItems
    response = MsgBox("Are you sure you want to delete " + TextBox.Text, vbYesNo, "Confirm Delete")
    If response = vbYes Then
        ListView.Items.Remove(i)
        goto Here
    End If
Next
Here:

顺便说一下,这是vb.net,请用vb.net标签编辑你的帖子。

答案 1 :(得分:0)

Step1更改下图所示的ListView1属性 enter image description here

Step2使用此代码:

Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
    Dim i As ListViewItem
    For Each i In ListView1.SelectedItems
        Select Case MsgBox("Are you sure you want to delete selected record?", MsgBoxStyle.YesNo, "Confirm Delete")
            Case MsgBoxResult.Yes
                ListView1.Items.Remove(i)
            Case MsgBoxResult.No
        End Select
    Next
End Sub

在我的项目中,我使用下面的代码(正在运行):

Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
    Dim i As ListViewItem
    For Each i In ListView1.SelectedItems
        Select Case MsgBox("คุณต้องการลบรายการขายสินค้าที่ถูกเลือกใช่ไหม", MsgBoxStyle.YesNo, "ยืนยันการลบรายการขายสินค้า")
            Case MsgBoxResult.Yes
                ListView1.Items.Remove(i)
                txt_barcode.Text = ""
                txt_barcode.Select()
                txt_total.Text = FormatNumber(total_price)
                txt_cost.Text = FormatNumber(total_buy)
                txt_profit.Text = FormatNumber(total_price) - FormatNumber(total_buy)
                txt_Quantity.Text = "1"
                lbl_BarcodeID.Text = ""
                lbl_Description.Text = ""
                lbl_Price.Text = ""
            Case MsgBoxResult.No
        End Select
    Next
End Sub