Click here to Skip to main content
16,016,570 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:

C#
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();
Posted
Updated 25-Mar-16 7:56am
Comments
BillWoodruff 25-Mar-16 20:51pm    
Why are you generating random ListItem values ?

1 solution

This line

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


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.

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


There is also a FindByText function if that's what you're after.
 
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