如何避免Listbox Arraylist中的重复?

时间:2015-10-24 11:36:01

标签: c# arraylist

protected void Button2_Click1(object sender, EventArgs e)
{
    string str = "";

    if (listboxl.SelectedIndex >= 0)
    {
        for (int i = 0; i < listboxl.Items.Count; i++)
        {
                if (listboxl.Items[i].Selected)
                {
                    if (!arraylist1.Contains(listboxl.Items[i]))
                    {
                        arraylist1.Add(listboxl.Items[i]);

                    }
                    //else if (listboxl.Items.Contains(listboxr.Items[i]))
                    //{
                    //    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Unit Already added !')", true);
                    //}
                }

        }
        for (int i = 0; i < arraylist1.Count; i++)
        {
             if (!listboxr.Items.Contains(((ListItem)arraylist1[i])))
            {
                listboxr.Items.Add(((ListItem)arraylist1[i]));

            }

             listboxl.Items.Remove(((ListItem)arraylist1[i])); 
        }

        listboxr.SelectedIndex = -1;
    }     
}

我使用此代码将项添加到列表中,但它允许重复值。请帮助我。

0 个答案:

没有答案