Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

listview duplicates

Posted on 2007-11-15
8
Medium Priority
?
842 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 2000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

662 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