Windows Forms ListView Controls Not working while cilck event

Hi,

I am using Windows Forms ListView Controls (ListView1 and ListView2).

pls. refer the screenshot for details.

When I click the button1, then the corresponding list1 selected item will transfer to Listview2.

for example, the first time I selected A, B, C and then click Button1. now the item will move to Listview2 control.

if I select the same item (A, B, C) again then click Button1, now and then the item should not come again in ListView2 controls.

the main concern is, to prevent the duplicate values in listview2.

Refer attached: Listview ScreenShot.jpg

my code

------------
 private void Button1_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in Listview1.SelectedItems)
            {
                if (item.Text  != null)
                {
                    Listview2.Items.Add(item.Text);
                }
            }
        }

Open in new window


PS: Pls. guide us how to prevent the duplicate values when I try to move the same values in listview2.
Listview-ScreenShot.png
Ganesh STech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You could use the FindItemWithText() method to check before adding to the 2nd ListView:

foreach (ListViewItem item in Listview1.SelectedItems)
{
    if (Listview2.FindItemWithText(item.Text) == null)
    {
        Listview2.Items.Add((ListViewItem)item.Clone());
    };
}

Open in new window

0
Fernando SotoRetiredCommented:
Hi Ganesh;

The following code will only add items to ListView2 from ListView1 if it does not already exist.
private void button1_Click(object sender, EventArgs e)
{
    if ( Listview1.SelectedItems.Count > 0 )
    {
        foreach ( ListViewItem item in Listview1.SelectedItems )
        {
            if ( !Listview2.Items.Cast<ListViewItem>().Any(t => t.Text == item.Text ))
            {
                Listview2.Items.Add(item.Text);
            }
        }
    }
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ganesh STech Lead cum developerAuthor Commented:
Thanks..it's working...
0
Fernando SotoRetiredCommented:
That’s great Ganesh, glad to help. Please don’t forget to close the question by assigning points.
0
Ganesh STech Lead cum developerAuthor Commented:
Thanks it's  working..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Listview

From novice to tech pro — start learning today.