【问题标题】:How do I list items from 3 separated listboxes if their item numbers are different?如果项目编号不同,如何从 3 个单独的列表框中列出项目?
【发布时间】:2021-07-16 14:31:49
【问题描述】:

你好!

任务:3 人在 3 家不同的酒店下榻时被谋杀。谋杀不是在同一天发生的。您有来自每家酒店的 3 个列表,其中包含住在那里的人的姓名。如果您发现匹配的名称,请将其列为嫌疑人。

private void button1_Click(object sender, EventArgs e)
{
    int index = 0;
    for (int i = 0; i < n2; i++) //I have 3 different 'n's, n2 is the largest, that is why I use it here (n - listBox1; n2 - listBox2; n3 - listBox3)
    {
        if (Convert.ToString(listBox1.Text[i]) == Convert.ToString(listBox2.Text[i]) || Convert.ToString(listBox1.Text[i]) == Convert.ToString(listBox3.Text[i]) || Convert.ToString(listBox2.Text[i]) == Convert.ToString(listBox3.Text[i]))
        {
            listBox4.Items.Add(goldensheep[index]);
            index++;
        }
    }
}

我有listBox1listBox2listBox3中列出的所有数据,目标是listBox4。 名称可以在数组中找到:“goldensheep”、“goldenbull”、“goldenostrich”,但我在这里只使用了“goldensheep”,因为我相信(d)这是唯一需要的。

当我点击 button1 时,我得到一个错误,但不幸的是我不知道为什么会发生这种情况。

提前致谢,祝你有美好的一天! :)

【问题讨论】:

  • 请告诉我们您看到的确切错误是什么。
  • “索引指向数组边界之外”——或者类似的,我的 VS 不是英文的。
  • 你能解释一下你使用Convert.ToString()的意图吗?
  • 并非如此。我想我必须转换它们才能比较它们。

标签: c# forms winforms


【解决方案1】:

我认为这样的事情可以解决问题。我还没有编译它们,所以要小心语法错误。我使用了 hashset,因为它会自动忽略以前存在的项目,因此它们不会在 listbox4 中重复。

private void button1_Click(object sender, EventArgs e)
{

    HashSet<string> Suspects = new HashSet<string>();

    for(int i=0; i< listBox1.Items.Count; i++)
    {
        string L1Name = (string)Listbox1.Items[i];
      
        for (int j=0; j< listBox2.Items.Count; j++)
        {
            if ((string)Listbox2.Items[j] == L1Name)
                Suspects.Add(L1Name);
        }

        for (int j = 0; j < listBox3.Items.Count; j++)
        {
            if ((string)Listbox3.Items[j] == L1Name)
                Suspects.Add(L1Name);
        }
    }

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        string L2Name = (string)listBox2.Items[i];              

        for (int j = 0; j < listBox3.Items.Count; j++)
        {
            if ((string)Listbox3.Items[j] == L2Name)
                Suspects.Add(L2Name);
        }
    }

    listBox4.Items.Clear();
    foreach (string susp in Suspects)
        listBox4.Items.Add(susp)
}

【讨论】:

    【解决方案2】:

    您收到错误是因为 listBox1listBox3 的最后一个索引小于 n2。您实际上只比较每个列表的相同索引的项目。

    我会addRange() 所有三个列表,distinct() 他们接收所有可能的名称。遍历这些名称以检查它们是否出现在所有三个列表中。

    【讨论】:

    • 不幸的是,我不知道该怎么做。我对编程有点陌生,甚至对表单应用程序编程也很陌生。
    【解决方案3】:

    这应该做你想做的:

    var lv2Items = from ListViewItem item in listView2.Items
              select item.Text;
    
    var lv3Items = from ListViewItem item in listView3.Items
              select item.Text;
    
    var commonNames = from ListViewItem item in listView1.Items
                where lv2Items.Contains(item.Text)
                where lv3Items.Contains(item.Text)
                select item;
    
    foreach(var item in commonNames)
    {
        ListViewItem commonName = new ListViewItem();
        commonName.Text = item.Text;
    
        for (int i = 0; i < item.SubItems.Count; i++)
        {
            commonName.SubItems.Add(item.SubItems[i].Text);
        }
        listView4.Items.Add(commonName);
    }
    

    lv2Itemslv3Items 拉取 listView2listView3 中每个项目的 Text 属性。 commonNames 然后从listView1 中选择项目,其中该项目的Text 属性出现在listView2listView3 中。末尾的foreachcommonNames 中的所有元素放入listView4

    无论每个列表的大小如何,这都应该有效,并且无论元素存储的顺序如何,它都可以工作。它仅在Text 属性的字符串值完全匹配时才有效,这是什么考虑这些是否是真实的客人名单而不是假设。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多