Solved

How do I Prevent duplicate subItems in a listview?

Posted on 2006-11-29
11
495 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 43
Getting multiple values from a dataset ASP.NET(VB) 3 49
VB.net/WinForms: adjust Screen Resolution 6 58
Run a batch file when a Crystal Report is opened 5 27
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

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