Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

listview duplicates

Posted on 2007-11-15
8
Medium Priority
?
844 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
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 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