Windows表单listview控件在cilck事件时不起作用 [英] Windows forms listview controls not working while cilck event

查看:95
本文介绍了Windows表单listview控件在cilck事件时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PS: Pls. guide us how to prevent the duplicate values when I try to move the same values in listview2.





我尝试过:





What I have tried:

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

推荐答案

你可以这样做:

You can do it like this:
foreach (ListViewItem item in listView1.SelectedItems)
{
    if (item.Text != null && !listView2.Items.ContainsKey(item.Text))
    {
        listView2.Items.Add(item.Text, item.Text, null);
    }
}



要从listview2中删除项目:


To remove items from listview2:

foreach (ListViewItem item in listView2.Items)
{
    if (listView2.SelectedItems.Contains(item))
    {
        listView2.Items.Remove(item);
    }
}


这篇关于Windows表单listview控件在cilck事件时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆