本文介绍了如何删除listbox2中listbox1中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您可以帮我删除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:List112345List 2251What 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();推荐答案if (!ListBox2.Items.Contains(listItems)) 正在检查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中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 00:30