【问题标题】:How to add items in listbox without duplicate Like That : [duplicate]如何在不重复的情况下在列表框中添加项目:[重复]
【发布时间】:2016-06-02 13:01:48
【问题描述】:

我试图在列表框中显示不重复的项目。

private void Lst_Box_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Chk_Multi.Checked == true)
    {
        Lst_Box.SelectionMode = SelectionMode.MultiSimple;
        if (Lst_Box.SelectedItem == "Janvier")
        {
            Lst_Selected.Items.Add(Lst_Box.Text);
        }
    }
    if (Chk_Multi.Checked == false)
    {
        Lst_Box.SelectionMode = SelectionMode.One;
        Lst_Selected.Items.Add(Lst_Box.Text);
    }
}

【问题讨论】:

  • 编写将使用.Contains()方法搜索列表框的代码,您需要先检查该值是否存在,然后再添加到列表框非常简单的任务..

标签: c# visual-studio listboxitem


【解决方案1】:
if(!Lst_Selected.Items.Contains(Lst_Box.Text)
{
  Lst_selected.Items.Add(Lst_Box.Text);
}

【讨论】:

  • 谢谢你成功了
【解决方案2】:

您可以使用Except 扩展方法...示例..

int[] foo = new int[]{1,2,3};
int[] bar = new int[]{1,3};

IEnumerable<int> fooMinusBar = foo.Except(bar);
IEnumerable<int> barMinusFoo = bar.Except(foo);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2017-03-29
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2019-06-26
    • 2016-08-21
    相关资源
    最近更新 更多