Solved

How do I Prevent duplicate subItems in a listview?

Posted on 2006-11-29
11
498 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
  • 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 500 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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