我试着比较两个列表框的已加工和未加工的项数和项名。
我想在listMachedItems中显示加工项目名称,在lblMachedItemsCount中显示加工项目计数。
并在listNotMachedItems中显示NotMached项名称,NotMached项在lblNotMachedItemsCount中计数。
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;
}
}
}发布于 2014-02-08 22:46:54
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";发布于 2014-02-08 22:47:07
listMatchedItems.Items.AddRange(list1.Intersect(list2).ToArray());
listMachedItemsCount = listMatchedItems.Count();
listNotMatchedItems.Items.AddRange(list1.Except(list2).ToArray());
listNotMachedItemsCount = listNotMatchedItems.Count();发布于 2014-02-09 00:55:29
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
{
}
}https://stackoverflow.com/questions/21647349
复制相似问题