Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to get values from listbox not in another list
Posted
Comments
Sergey Alexandrovich Kryukov 30-Jul-15 0:46am    
ListBox? Which one? Full type name, please.
And what's the problem?
—SA
[no name] 30-Jul-15 0:47am    
What you have tried?
Eg: string text = listBox1.GetItemText(listBox1.SelectedItem);
Anil Vaghasiya 30-Jul-15 0:50am    
Hello,
Please describe the problem or issue in details??

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
 
Share this answer
 
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.
C#
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:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900