Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

How do I Prevent duplicate subItems in a listview?

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
Avatar of YZlat
YZlat
Flag of United States of America image

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

     
Avatar of Todd MacPherson

ASKER

I do not understand what you are saying. Please explain.
ASKER CERTIFIED SOLUTION
Avatar of riyazthad
riyazthad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of riyazthad
riyazthad

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
Very goog thanks dude.

PBLack
goog? my bad!

PBLack
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
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
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
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.