vb.net:如何检测listview中的重复项?

时间:2017-01-11 14:54:55

标签: vb.net

我的程序中有一个组合框,您可以从选项中选择一个,当您在金额文本框中按Enter键时,数据将被传输到列表视图...唯一的问题是重复项的出现

Public Class Form1

    Dim listitem As ListViewItem
    Dim amount As Decimal
    Dim total As Decimal
    Dim subtract As Decimal
    Dim x As Long
    Dim y As Long

    Private Sub textAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textAmount.KeyDown

        total = Val(textTotal.Text)
        amount = Val(textAmount.Text)
        textAmount.MaxLength = 9

        If e.KeyCode = Keys.Enter Then
            If IsNumeric(textAmount.Text) = True Then
                If amount > 0 = True Then

                   'Need to put something here to prevent the duplicates 
                   'from being entered into the List View

                    total = total + amount
                    listitem = New ListViewItem
                    listitem.Text = comboCharge.Text
                    listitem.SubItems.Add(textAmount.Text)
                    listitem.SubItems.Add(textName.Text)
                    listitem.SubItems.Add(textAddress.Text)
                    ListView1.Items.Add(listitem)
                    textTotal.Text = total

                    MsgBox("Invalid input", MsgBoxStyle.Exclamation, "ERROR!")

                Else    
                    MsgBox("Invalid input", MsgBoxStyle.Exclamation, "ERROR!")
                End If
            Else
                MsgBox(x, MsgBoxStyle.Exclamation, "ERROR!")
            End If
        End If
    End Sub

    Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown
        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
            subtract = Val(ListView1.SelectedItems(0).SubItems(1).Text)
            total = total - subtract
            textTotal.Text = total
            ListView1.SelectedItems(0).Remove()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        textTotal.Text = 0.0
        textName.Text = "Enter full name"
        textAddress.Text = "Enter full address"
    End Sub

End Class

2 个答案:

答案 0 :(得分:1)

如果我了解您尝试做的事情,那么您不会尝试多次添加费用。我认为这会有所帮助:

首先看看这个Function

Private Function chargeExists(ByVal text As String) As Boolean

    For Each lvi As ListViewItem In ListView1.Items

        If lvi.Text.Equals(text) Then Return True

    Next

    Return False

End Function

这是您使用Function

的方法
If Not chargeExists(comboCharge.Text) Then
    total = total + amount
    listitem = New ListViewItem
    listitem.Text = comboCharge.Text
    listitem.SubItems.Add(textAmount.Text)
    listitem.SubItems.Add(textName.Text)
    listitem.SubItems.Add(textAddress.Text)
    ListView1.Items.Add(listitem)
    textTotal.Text = total
Else
    MessageBox.Show("Duplicate", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

您的代码看起来与此类似:

Private Sub textAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textAmount.KeyDown

    total = Val(textTotal.Text)
    amount = Val(textAmount.Text)
    textAmount.MaxLength = 9

    If e.KeyCode = Keys.Enter Then
        If IsNumeric(textAmount.Text) = True Then
            If amount > 0 = True Then
                If Not chargeExists(comboCharge.Text) Then
                    total = total + amount
                    listitem = New ListViewItem
                    listitem.Text = comboCharge.Text
                    listitem.SubItems.Add(textAmount.Text)
                    listitem.SubItems.Add(textName.Text)
                    listitem.SubItems.Add(textAddress.Text)
                    ListView1.Items.Add(listitem)
                    textTotal.Text = total
                Else
                    MessageBox.Show("Duplicate", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MsgBox("Invalid input", MsgBoxStyle.Exclamation, "ERROR!")
            End If
        Else
            MsgBox(x, MsgBoxStyle.Exclamation, "ERROR!")
        End If
    End If
End Sub

另外,我会使用MessageBox.Show,而不是MsgBox

答案 1 :(得分:0)

显然我厌倦了在这件事上寻找某种捷径......所以我所做的就是这个...

Public Class Form1
Dim listitem As ListViewItem
Dim amount As Decimal
Dim total As Decimal
Dim subtract As Decimal
Dim x As Long
Dim y As Long
Dim charge(90) As String
Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long

Private Sub textAmount_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textAmount.KeyDown
    total = Val(textTotal.Text)
    amount = Val(textAmount.Text)
    textAmount.MaxLength = 9
    If e.KeyCode = Keys.Enter Then
        If IsNumeric(textAmount.Text) = True Then
            If amount > 0 = True Then


                For y = 0 To x Step -1
                    charge(y) = comboCharge.Text

添加了大量的条件语句...... 如果您有很多选择来处理

,则不建议这样做
                    If charge(y) = "Delivery Fee" Then
                        If a < 1 Then
                            total = total + amount
                            listitem = New ListViewItem
                            listitem.Text = comboCharge.Text
                            listitem.SubItems.Add(textAmount.Text)
                            listitem.SubItems.Add(textName.Text)
                            listitem.SubItems.Add(textAddress.Text)
                            ListView1.Items.Add(listitem)
                            textTotal.Text = total
                            a = a + 1
                        Else
                            MsgBox("Duplicate", MsgBoxStyle.Critical, "Error!")
                        End If
                    ElseIf charge(y) = "Cancellation Fee" Then
                        If b < 1 Then
                            total = total + amount
                            listitem = New ListViewItem
                            listitem.Text = comboCharge.Text
                            listitem.SubItems.Add(textAmount.Text)
                            listitem.SubItems.Add(textName.Text)
                            listitem.SubItems.Add(textAddress.Text)
                            ListView1.Items.Add(listitem)
                            textTotal.Text = total
                            b = b + 1
                        Else
                            MsgBox("Duplicate", MsgBoxStyle.Critical, "Error!")
                        End If

                    ElseIf charge(y) = "Special Handling Fee" Then
                        If c < 1 Then
                            total = total + amount
                            listitem = New ListViewItem
                            listitem.Text = comboCharge.Text
                            listitem.SubItems.Add(textAmount.Text)
                            listitem.SubItems.Add(textName.Text)
                            listitem.SubItems.Add(textAddress.Text)
                            ListView1.Items.Add(listitem)
                            textTotal.Text = total
                            c = c + 1
                        Else
                            MsgBox("Duplicate", MsgBoxStyle.Critical, "Error!")
                        End If

                    ElseIf charge(y) = "Assistance Fee" Then
                        If d < 1 Then
                            total = total + amount
                            listitem = New ListViewItem
                            listitem.Text = comboCharge.Text
                            listitem.SubItems.Add(textAmount.Text)
                            listitem.SubItems.Add(textName.Text)
                            listitem.SubItems.Add(textAddress.Text)
                            ListView1.Items.Add(listitem)
                            textTotal.Text = total
                            d = d + 1
                        Else
                            MsgBox("Duplicate", MsgBoxStyle.Critical, "Error!")
                        End If

                    ElseIf charge(y) = "Installation Fee" Then
                        If x < 1 Then
                            total = total + amount
                            listitem = New ListViewItem
                            listitem.Text = comboCharge.Text
                            listitem.SubItems.Add(textAmount.Text)
                            listitem.SubItems.Add(textName.Text)
                            listitem.SubItems.Add(textAddress.Text)
                            ListView1.Items.Add(listitem)
                            textTotal.Text = total
                            x = x + 1
                        Else
                            MsgBox("Duplicate", MsgBoxStyle.Critical, "Error!")
                        End If
                    End If
                Next
            Else

                MsgBox("Invalid input", MsgBoxStyle.Exclamation, "ERROR!")
            End If
        Else
            MsgBox("Enter numbers only!", MsgBoxStyle.Exclamation, "ERROR!")
        End If
    End If
End Sub
Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown
    If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
        subtract = Val(ListView1.SelectedItems(0).SubItems(1).Text)
        total = total - subtract
        textTotal.Text = total
        ListView1.SelectedItems(0).Remove()
    End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    textTotal.Text = 0.0
    textName.Text = "Enter full name"
    textAddress.Text = "Enter full address"
    comboCharge.Text = "Select type of charge"
    textAmount.Text = "0.0"
End Sub

Private Sub buttonclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonclear.Click
    textTotal.Text = 0.0
    textName.Text = "Enter full name"
    textAddress.Text = "Enter full address"
    comboCharge.Text = "Select type of charge"
    textAmount.Text = "0.0"
    subtract = Val(ListView1.Items(0).SubItems(1).Text)
    total = total - subtract
    textTotal.Text = total
    ListView1.Items(0).Remove()

  End Sub
 End Class