Solved

listview duplicates

Posted on 2007-11-15
8
840 Views
Last Modified: 2013-12-17
how t o check for duplicates in a listview while adding it from a listbox
0
Comment
Question by:kranthi4uonly
[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
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20291985
you can check by using ListView.public ListViewItem FindItemWithText() method.
It can search for a text in the first column, and there are other versions to check in other columns. I suggest to use the first one.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20291990
Sorry, I meant:
you can check by using ListView.FindItemWithText() method.
0
 
LVL 3

Expert Comment

by:longtruong
ID: 20295657
Hi,

You should use ListView.FindItemWithText() as suggested by jaime_olivares.  However, I am not sure if it is enough so I would like to give some additional information here.  If the item text is unique then this method is OK.  However, if the list has several items with the same text, this method always returns the first matched item.  To solve this situation, I suggest that when adding an item to the list, please do not forget associating a unique data to Tag attribute of each item and the Tag's value will be used as a key to search for a duplicate later.

Attached sample code is provided.

Hope it helps.

Long
public bool CheckDuplicate(int id)
{
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        ListViewItem lvi = listView1.Items[i];
        if (lvi.Tag == (int)id)
        {
            return true;
        }
    }
    return false;
}
 
private void button1_Click(object sender, EventArgs e)
{
    //Check for duplicate
    if (CheckDuplicate(<your id here>) == false)
    {
        //Add new item to the list
        ListViewItem lvi = new ListViewItem();
        lvi.Text = "your item text here";
        lvi.Tag = "your id here";
        listView1.Items.Add(lvi);
    }
}

Open in new window

0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 

Author Comment

by:kranthi4uonly
ID: 20296529
hi longtruong:
i dont have duplicates in listview
iam adding it from a listbox into list view
iam using the following code but why it is not working i cant understand


private void button1_Click(object sender, EventArgs e)
        {
           
            ListViewItem item = new ListViewItem(listBox1.SelectedItem.ToString());
            for (int i = 0; i < listView1.Items.Count; i++)
            {

                ListViewItem s =new ListViewItem(item.Text);
                if (listView1.Items[i].Text.Contains(s.Text))
                {
                    break ;
                }
                //else
                {

                    listView1.Items.AddRange(new ListViewItem[] { item });
                    break;


                }
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20296567
what about my suggestion?
0
 

Author Comment

by:kranthi4uonly
ID: 20296650
yeah iam using ur code as like this but its saying object reference not set to an instance of object this works fien if their are duplicates
if not it gives exception as mentioned above
am i doing right
private void button1_Click(object sender, EventArgs e)
        {
           
            ListViewItem item = new ListViewItem(listBox1.SelectedItem.ToString());
            for (int i = 0; i < listView1.Items.Count; i++)
            {

                ListViewItem s =new ListViewItem(item.Text);
                ListViewItem j = listView1.FindItemWithText(s.Text);
                if (j.Text==s.Text && j!=null)
                {
                   
                    break ;
                }
                //else
                {
                    listView1.Items.AddRange(new ListViewItem[] { item });
                   
                    break;


                }
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20296658
It is quiet simple:
private void button1_Click(object sender, EventArgs e)
{
    string text = listBox1.SelectedItem.ToString();
    if (listView1.FindItemWithText(text) == null)  // it is not duplicated
    {
         ListViewItem item = new ListViewItem(text);
         listView1.Items.Add(item);
    }
}

Open in new window

0
 

Author Closing Comment

by:kranthi4uonly
ID: 31409411
excelent
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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

632 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