Solved

listview duplicates

Posted on 2007-11-15
8
835 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
Comment Utility
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
Comment Utility
Sorry, I meant:
you can check by using ListView.FindItemWithText() method.
0
 
LVL 3

Expert Comment

by:longtruong
Comment Utility
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
 

Author Comment

by:kranthi4uonly
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
what about my suggestion?
0
 

Author Comment

by:kranthi4uonly
Comment Utility
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
Comment Utility
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
Comment Utility
excelent
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

8 Experts available now in Live!

Get 1:1 Help Now