• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

adding items to a Listview problem

I wrote a program that manages serial keys, writes them to the registry in a strong encryption and reads them back etc.  When I add an application to the listview, the first two go alright then each one I add afterwords makes one additional entry (2, then 3 etc).  Below is the code when the add button is clicked:

=====
    Private Sub add_application()
        Dim i As Integer
        Try
            If appNameBox.Text = Nothing Then
                MsgBox("Please enter an application name", MsgBoxStyle.OKOnly, "Missing Name")
                appNameBox.Focus()
                Exit Sub
            ElseIf appSerialBox.Text = Nothing Then
                MsgBox("Please enter a serial key", MsgBoxStyle.OKOnly, "Missing Serial")
                appSerialBox.Focus()
                Exit Sub
            Else
                apName = appNameBox.Text
                apSerial = appSerialBox.Text
                existing_application()
            End If
            appNameBox.Text = Nothing : appSerialBox.Text = Nothing
        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try
        saved = False
    End Sub

 Private Sub existing_application()
        ''weird..
        Dim lvi As ListViewItem
        If Not appList.Items.Count = 0 Then
            ' For i = 0 To appList.Items.Count - 1
            For Each lvi In appList.Items
                If apName = appList.Items.Item(i).Text Then
                    Select Case MsgBox("Previous entry found.  Update it?", MsgBoxStyle.YesNo, "Update Entry")
                        Case MsgBoxResult.Yes
                            Dim item As New ListViewItem
                            item.Text = apName
                            item.SubItems.Add(apSerial)
                            appList.Items.Item(i).Selected = True
                            appList.Items.Remove(appList.SelectedItems.Item(0))
                            appList.Items.Add(item)
                            Exit Sub
                        Case MsgBoxResult.No
                            Exit Sub
                    End Select
                Else
                    Dim item As New ListViewItem
                    item.Text = apName
                    item.SubItems.Add(apSerial)
                    appList.Items.Add(item)
                End If
            Next
        Else
            Dim item As New ListViewItem
            item.Text = apName
            item.SubItems.Add(apSerial)
            appList.Items.Add(item)
        End If
        saved = False
    End Sub

=====

What did I do wrong?
0
Ander5
Asked:
Ander5
1 Solution
 
GrnEggsAndHamCommented:
try this...

        If Not appList.Items.Count = 0 Then
            ' For i = 0 To appList.Items.Count - 1
            Dim found As Boolean = False
            For Each lvi In appList.Items
                If apName = appList.Items.Item(i).Text Then
                    found = True
                    Exit For
                End If
            Next
            If found = True Then
                Select Case MsgBox("Previous entry found.  Update it?", MsgBoxStyle.YesNo, "Update Entry")
                    Case MsgBoxResult.Yes
                        Dim item As New ListViewItem
                        item.Text = apName
                        item.SubItems.Add(apSerial)
                        appList.Items.Item(i).Selected = True
                        appList.Items.Remove(appList.SelectedItems.Item(0))
                        appList.Items.Add(item)
                        Exit Sub
                    Case MsgBoxResult.No
                        Exit Sub
                End Select
            Else
                Dim item As New ListViewItem
                item.Text = apName
                item.SubItems.Add(apSerial)
                appList.Items.Add(item)
            End If
        Else
            Dim item As New ListViewItem
            item.Text = apName
            item.SubItems.Add(apSerial)
            appList.Items.Add(item)
        End If
0
 
Ander5Author Commented:
That works, I also devised my own method that works.  I'll give you the points regardless.

Thanks.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now