?
Solved

How do I Prevent duplicate subItems in a listview?

Posted on 2006-11-29
11
Medium Priority
?
513 Views
Last Modified: 2012-08-13
I can write code to prevent a duplicate item in a listview from being added but I want to prevent duplicate subitems from being placed in a listview.

Here is what I have and hopefully someone can modify it for me.

        Dim lstAttItem As ListViewItem = New ListViewItem(Trim(txtName.Text))
        Dim lvDup As ListViewItem
        For Each lvDup In lstMenu.Items
            If UCase(Trim(lvDup.Text)) = UCase(Trim(txtName.Text)) Then
                Beep()
                MsgBox("Duplicate Values are not permitted. Please fix.", MsgBoxStyle.Information, "ListView Error")
                txtName.Focus()
                Exit Sub
            End If
        Next

        lstMenu.Items.Add(lstAttItem)
        lstAttItem.SubItems.Add(Trim(txtCode1.Text)) 'need code to prevent the same subitem being added to this column
        lstAttItem.SubItems.Add(Trim(txtCode2.Text)) 'need code to prevent the same subitem being added to this column

'note this is a 3 column listview. The same item could exist as a subitem in another column. A duplicate subitem could exist in column 2 and 3 it just could not exist twice in the same column.

for example:

John, J, 123
J, 123, John
Tom, J, 678
Tim, P, 123
Bill,456,J

Rows 1,2 and 5 could be added to listview. Row's 3 and 4 have conflicts thus should be rejected.

Any help appreciated.

Thanks

PBLack
0
Comment
Question by:PBLack
[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
  • 4
  • 3
  • 3
11 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 18040368
Dim lstAttItem As ListViewItem = New ListViewItem(Trim(txtName.Text))
        Dim lvDup As ListViewItem
        For Each lvDup In lstMenu.Items
            If UCase(Trim(lvDup.Text)) = UCase(Trim(txtName.Text)) Then
                Beep()
                MsgBox("Duplicate Values are not permitted. Please fix.", MsgBoxStyle.Information, "ListView Error")
                txtName.Focus()
                Exit Sub
            Else
            ''add items here
               lstMenu.Items.Add(lstAttItem)
        lstAttItem.SubItems.Add(Trim(txtCode1.Text)) 'need code to prevent the same subitem being added to this column
        lstAttItem.SubItems.Add(Trim(txtCode2.Text)) 'need code to prevent the same subitem being added to this colum


            End If
        Next

     
0
 

Author Comment

by:PBLack
ID: 18040403
I do not understand what you are saying. Please explain.
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 2000 total points
ID: 18040921
Try this code.


      Dim lstAttItem As ListViewItem = New ListViewItem(Trim(txtName.Text))
      Dim lvDup As ListViewItem
      Dim blnDuplicate As Boolean

        For Each lvDup In lstMenu.Items
          If txtCode1.Text.Trim = i.SubItems(1).Text.Trim OrElse txtCode1.Text.Trim = i.SubItems(2).Text.Trim Then
                 Beep()
                MsgBox("Duplicate Values are not permitted. Please fix.", MsgBoxStyle.Information, "ListView Error")
                txtName.Focus()
                blnDuplicate = True
                Exit For
            End If
        Next

               
   
        If Not blnDuplicate Then
         lstMenu.Items.Add(lstAttItem)
        lstAttItem.SubItems.Add(Trim(txtCode1.Text)) 'need code to prevent the same subitem being added to this column
        lstAttItem.SubItems.Add(Trim(txtCode2.Text)) 'need code to prevent the same subitem being added to this colum
        End If
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:riyazthad
ID: 18040942
sorry

change i with lvDup in for loop.

if txtCode1.Text.Trim = i.SubItems(1).Text.Trim OrElse txtCode1.Text.Trim = i.SubItems(2).Text.Trim

to

if txtCode1.Text.Trim = lvDup .SubItems(1).Text.Trim OrElse txtCode1.Text.Trim = lvDup .SubItems(2).Text.Trim
0
 

Author Comment

by:PBLack
ID: 18044567
Very goog thanks dude.

PBLack
0
 

Author Comment

by:PBLack
ID: 18044573
goog? my bad!

PBLack
0
 
LVL 35

Expert Comment

by:YZlat
ID: 18046984
PBLack, I just posted the code with some changes so it'd work.

Basically my solution is the same as riyazthad's but you gave him all the points even though I posted first
0
 

Author Comment

by:PBLack
ID: 18065455
I disagree with this request 100%. I used the code of the other expert because he was the first to post a correct solution. The first expert comment only added the phrase ''add items here with no code. I asked for an explanation. None was given and a solution was given after the fact.

My record on this site is irrefutable and I stand by my decison. Quite frankly I am PO'd with respect to this complaint and want all comments regarding it removed from this thread.

PBLack
0
 
LVL 35

Expert Comment

by:YZlat
ID: 18068718
Objection!

Before you looped throught the items to check for duplicates

 For Each lvDup In lstMenu.Items
            If UCase(Trim(lvDup.Text)) = UCase(Trim(txtName.Text)) Then
                Beep()
                MsgBox("Duplicate Values are not permitted. Please fix.", MsgBoxStyle.Information, "ListView Error")
                txtName.Focus()
                Exit Sub
            End If
        Next

and then after you added all the items regardless if they have duplicates

        lstMenu.Items.Add(lstAttItem)
        lstAttItem.SubItems.Add(Trim(txtCode1.Text)) 'need code to prevent the same subitem being added to this column
        lstAttItem.SubItems.Add(Trim(txtCode2.Text)) 'need code to prevent the same subitem being added to this column

I added ELSE to your if statement and moved the code for adding items there, inside the FOR loop so that it would only add the items if they are not duplicates


Here, check my changes once more:


Dim lstAttItem As ListViewItem = New ListViewItem(Trim(txtName.Text))
Dim lvDup As ListViewItem
For Each lvDup In lstMenu.Items
      If UCase(Trim(lvDup.Text)) = UCase(Trim(txtName.Text)) Then
                Beep()
                MsgBox("Duplicate Values are not permitted. Please fix.", MsgBoxStyle.Information, "ListView Error")
                txtName.Focus()
                Exit Sub
       Else
                ''add items here
                   lstMenu.Items.Add(lstAttItem)
              lstAttItem.SubItems.Add(Trim(txtCode1.Text)) 'need code to prevent the same subitem being added to this column
              lstAttItem.SubItems.Add(Trim(txtCode2.Text)) 'need code to prevent the same subitem being added to this colum


       End If
Next
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 18069024
Dear YZLat

Take this example

John, J, 123
J, 123, John
Tom, J, 678
Tim, 123, 123
Bill,456,J

John, J, 123 will add successfully using ur for loop
J, 123, John will add successfully using ur for loop
Tom, J, 678 will popup message

so far fine

Tim, 123, 123 will add item to list bcos there is mismatch with first item.

But it is duplicate with second item "J, 123, John" in second column

In second loop I will get a message , but first loop added to list.

YZlat , I dont know I am wrong.






0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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