C# 如何比较C中两个列表框的项目名称?
我试着比较两个列表框的加工和非加工项目数和项目名称 我想在listMachedItems中显示加工项目名称,在LblMachedItemCount中显示加工项目计数 并在列表NOTMACHEDITEMS中显示未加工项目名称,在LBLNOTMACHEDITEMSCONT中显示未加工项目计数C# 如何比较C中两个列表框的项目名称?,c#,.net,winforms,C#,.net,Winforms,我试着比较两个列表框的加工和非加工项目数和项目名称 我想在listMachedItems中显示加工项目名称,在LblMachedItemCount中显示加工项目计数 并在列表NOTMACHEDITEMS中显示未加工项目名称,在LBLNOTMACHEDITEMSCONT中显示未加工项目计数 请删除代码中不必要的空行好吗?请删除代码中不必要的空行好吗?我看不到项目Source,Intersect,Exception。我使用System.Windows.Forms;我看不到ItemsSource、In
请删除代码中不必要的空行好吗?请删除代码中不必要的空行好吗?我看不到项目Source,Intersect,Exception。我使用System.Windows.Forms;我看不到ItemsSource、Intersect,除了。我使用System.Windows.Forms;'System.Windows.Forms.ListBox'不包含'ItemsSource'的定义,并且找不到接受'System.Windows.Forms.ListBox'类型的第一个参数的扩展方法'ItemsSource'。是否缺少using指令或程序集引用?'System.Windows.Forms.ListBox'不包含'ItemsSource'的定义,并且没有可以找到接受类型为“System.Windows.Forms.ListBox”的第一个参数的扩展方法“ItemsSource”。是否缺少using指令或程序集引用?
int x = 0;
int y = 0;
for (int i = 0; i < listBox1.Items.Count; i++)
{
for (int j = 0; j < listBox2.Items.Count; j++)
{
if (listBox1.Items[i] == listBox2.Items[j])
{
y++;
//found mached items
//lblMachedItemsCount.Text = y.ToString() + " " + "items are mached";
// listMachedItems.Items.Add(listBox1.Items[i].ToString());
break;
}
else
{
x++;
//lblNotMachedItemsCount.Text = x.ToString() + " " + "items are not mached";
// listNotMachedItems.Items.Add(listBox1.Items[i].ToString());
break;
}
}
}
listMachedItems.ItemsSource = listBox1.Items.Where(x => listBox2.Items.Contains(x));
listNotMachedItems.ItemsSource = listBox1.Items.Where(x => !listBox2.Items.Contains(x));
lblNotMachedItemsCount.Text = listNotMachedItems.Count() + " items are not matched";
lblMachedItemsCount.Text = listMachedItems.Count() + " items are matched";
listMatchedItems.Items.AddRange(list1.Intersect(list2).ToArray());
listMachedItemsCount = listMatchedItems.Count();
listNotMatchedItems.Items.AddRange(list1.Except(list2).ToArray());
listNotMachedItemsCount = listNotMatchedItems.Count();
var list1 = Enumerable.Intersect(listBox1.Items.Cast<string>().ToArray(), listBox2.Items.Cast<string>().ToArray());
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (list1.Contains(listBox1.Items[i]))
{
listBox1.SetSelected(i, true);
listMatchedItems.Items.Add(listBox1.Items[i]).ToString();
}
else
{
listNotMatchedItems.Items.Add(listBox1.Items[i]).ToString();
}
}
var list2 = Enumerable.Intersect(listBox2.Items.Cast<string>().ToArray(), listBox1.Items.Cast<string>().ToArray());
for (int i = 0; i < listBox2.Items.Count; i++)
{
if (list2.Contains(listBox2.Items[i]))
{
int a = i + 1;
listBox2.SetSelected(i, true);
}
else
{
}
}