?
Solved

adding items to a Listview problem

Posted on 2005-03-31
2
Medium Priority
?
192 Views
Last Modified: 2010-04-23
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
Comment
Question by:Ander5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
GrnEggsAndHam earned 2000 total points
ID: 13677161
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
 

Author Comment

by:Ander5
ID: 13677203
That works, I also devised my own method that works.  I'll give you the points regardless.

Thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question