Solved

How do I Prevent duplicate subItems in a listview?

Posted on 2006-11-29
11
490 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now