Solved

listview duplicates

Posted on 2007-11-15
8
839 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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