如何从列表框中获取值不在另一个列表中 [英] how to get values from listbox not in another list

查看:93
本文介绍了如何从列表框中获取值不在另一个列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从列表框中获取值不在另一个列表中

解决方案

这个问题没有任何意义。 ListBox 不是列表,因此另一个列表没有意义。更重要的是,在简单的名称 ListBox 下有不同的不相关类型,所以我不能给你关于该做什么的确切建议,特别是因为你没有解释你的问题是什么(除了明显无法阅读原始MSDN文档)。



但是你可能不需要太多细节;这个想法是:一个列表框可以绑定或不。对于您正在使用的类型,请阅读文档以找到表示绑定中使用的数据对象的类型成员,这通常是一些集合。如果它不为null,则表示可通过绑定访问的列表项集。由于这是引用类型对象,该成员的副本是引用,因此它不能是另一个列表(如果您不理解引用类型和引用,您应该停止任何UI开发尝试并返回非常基础)。



如果它为null,那么控件不受约束,读取相同的文档并找到表示列表项集合的属性。集合元素的运行时类型通常是 System.Object ,因此您必须键入元素数据。 (如果您不了解运行时类型或转换,请参见上文。)



-SA


< blockquote>我完全赞同@Sergey Alexandrovich Kryukov,我的建议是在发布问题之前尝试学习如何提出其他人可以理解的问题。



现在这个解决方案有什么新东西?

我可以用两种方式来回答你的问题。

问。如何从列表中获取C#中另一个列表中不存在的值?

答案:基本上,如何比较C#中的两个列表。 />
注意:C#中的列表代表可以通过索引访问的强类型对象列表。

  var  firstNotSecond = list1.Except( list2)。ToList(); 



参考: http:// stackoverflow。 com / a / 12795900/1006297 [ ^ ]



问。如何从列表框中获取C#中另一个列表框中不存在的项目值?

答案:

列表与LT;串GT; myList = new List< string>();
for int i = 0 ; i < listbox1.Items.Count; i ++)
{
for int j = 0 ; j < listbox2.Items.Count; j ++)
{
if (listbox2.Items [j] .ToString()== listbox1。 Items [i] .ToString())
{
break ;
}
else if ((j + 1)== listbox2.Items .Count)
{
myList.Add(listbox2.Items [j] .ToString())
}
}
} < / 字符串 > < / string >



循环结束后,您可以在 myList 中获取所需的值列表。



希望,它有助于:)

如果您的要求与此不同,请尝试在您的问题中添加更多信息。


how to get values from listbox not in another list

解决方案

The question does not really make any sense. ListBox is not a list, so "another list" is meaningless. More importantly, there are different unrelated types under the simple name ListBox, so it's not my fault that I cannot give you exact advice on what to do, and especially because you did not explain what's your problem (except the apparent failure to read original MSDN documentation).

But perhaps you don't need much detail; the idea is: a list box can be bound or not. For the type you are using, read the documentation to locate the type member representing the data object used in binding, which is typically some collection. If it is not null, it represents the set of list items accessible through binding. As this is the reference-type object, the copy of this member is reference, so it cannot be "another list" (and if you don't understand reference types and references, you should just stop any attempts of UI development and get back to the very basics).

If it's null, so the control is not bound, read the same documentation and locate the property representing collection of list items. The runtime type of the collection element is typically System.Object, so you have to type-cast the element data. (If you don't understand runtime types or casting, see above.)

—SA


I completely agree with @Sergey Alexandrovich Kryukov and my advice to you would be before posting a question just try to learn how to ask a question which can be understood by others.

Now what new in this solution?
I can assume your question in two way.
Q. How to get values from a list which are not present in another list in C#?
Ans: Basically, how to compare two lists in C#.
Note: List in C# represents a strongly typed list of objects that can be accessed by index.

var firstNotSecond = list1.Except(list2).ToList();


Reference: http://stackoverflow.com/a/12795900/1006297[^]

Q. How to get values of items from a listbox which are not present in another listbox in C#?
Ans:

List<string> myList = new List<string>();
for(int i = 0; i < listbox1.Items.Count; i++)
  {
       for(int j = 0; j < listbox2.Items.Count; j++)
       {
             if (listbox2.Items[j].ToString() == listbox1.Items[i].ToString())
             {
                 break;
             }
             else if((j+1)==listbox2.Items.Count)
             {
                myList.Add(listbox2.Items[j].ToString())
             }
       }
 }</string></string>


After the loop is finished you can get the required list of values in myList.

Hope, it helps :)
In case your requirement is something different than this, please try adding more information to your question.


这篇关于如何从列表框中获取值不在另一个列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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