如何删除listbox2中listbox1中的项目? [英] How to remove the items in listbox1 that is in listbox2?

查看:107
本文介绍了如何删除listbox2中listbox1中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以帮我删除ListBox1中ListBox2上相同值的项目吗?



我尝试过的输出是这样的:



清单1

1

2

3

4

5



清单2

2

5

1



我尝试过:



这是我的代码:



Hi can you help me remove the items in ListBox1 with the same value on ListBox2?

The output of what I have tried is like this:

List1
1
2
3
4
5

List 2
2
5
1

What I have tried:

Here is my code:

ListBox1.Items.Add(filename);

Label1.Text = ListBox1.Items.Count.ToString();

int randVal = rand.Next(z.Length);
string y;

if (ListBox2.Items.Count <= 4)
{
    y = z[randVal].ToString();

    ListItem listItems = new ListItem(y);

    if (!ListBox2.Items.Contains(listItems))
    {
         ListBox2.Items.Add(y);
    }
}

Label2.Text = ListBox2.Items.Count.ToString();

推荐答案

这一行



This line

if (!ListBox2.Items.Contains(listItems))

< br $>


正在检查Items集合是否包含listItems对象,而不是刚刚创建它的那个,你还没有将它添加到Items集合中。你真正想要做的是检查ListBox2.Items是否有一个ListItem,其值为y。





is checking if the Items collection contains the listItems object, which is doesn't as you have just created it, you haven't added it to the Items collection. What you really want to do is check if ListBox2.Items has a ListItem that has "y" as it's value.

if (ListBox2.Items.FindByValue(listItems.Value) == null)





还有一个FindByText函数,如果这就是你所追求的。



There is also a FindByText function if that's what you're after.


这篇关于如何删除listbox2中listbox1中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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