避免重复列表项

时间:2013-01-23 20:37:16

标签: c# validation combobox listbox

我有一个ListBox,其中包含我从ComboBox中选择的项目。当我从ComboBox中选择一个项目时,它也会删除ComboBox中的该项目。

我的表单上有一个按钮,让我复制ListBox中的所选项目,在ListBox中为我提供两个相同的项目。

我的表单上还有一个按钮,让我从ListBox中删除所选项目。如果我从ListBox中移除某个项目,该项目将返回ComboBox,但如果我复制了该项目,并且我从ListBox中移除了这两个项目,则它们都会显示在ComboBox

我不希望在ComboBox

中有两个相同的项目

以下是我尝试使用的代码:

private void buttonRemove_Click(object sender, EventArgs e)
    {
        try
        {
            if (ComboBox.ToString().Contains("Chicken McNuggets"))
            {
                found = true;
            }
            if (!found)
            {
                ComboBox.Items.Add("Chicken McNuggets");
                found = false;
            }
            ListBox.Items.Remove(ListBox.SelectedItem);
        }
        catch
        {
            MessageBox.Show(// Message);
        }
    }

这是我第一次在这里提问。

编辑:

private void buttonRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if ((String)listBox.SelectedItem == "Big Mac" || (String)listBox.SelectedItem == "Quarter Pounder" || (String)listBox.SelectedItem == "McFeast" || (String)listBox.SelectedItem == "Cheeseburger" || (String)listBox.SelectedItem == "Hamburger" || (String)listBox.SelectedItem == "Big Tasty Bacon" || (String)listBox.SelectedItem == "McChicken" || (String)listBox.SelectedItem == "Fillet-O-Fish" || (String)listBox.SelectedItem == "Chicken Nuggets")
                {
                    comboBox.Items.Add(listBox.SelectedItem);
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                else if ((String)listBox.SelectedItem == "BBQ dip" || (String)listBox.SelectedItem == "Cheddar dip" || (String)listBox.SelectedItem == "Gulerod" || (String)listBox.SelectedItem == "Hvidløgs dip" || (String)listBox.SelectedItem == "Karry dip" || (String)listBox.SelectedItem == "Ketchup" || (String)listBox.SelectedItem == "Pommes Frites Sauce" || (String)listBox.SelectedItem == "Sennep dip" || (String)listBox.SelectedItem == "Sursød dip" || (String)listBox.SelectedItem == "Æbler")
                {
                    comboBox2.Items.Add(listBox.SelectedItem);
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                else if ((String)listBox.SelectedItem == "Gulerodskage" || (String)listBox.SelectedItem == "Kanelsnegl" || (String)listBox.SelectedItem == "McDonut chokolade" || (String)listBox.SelectedItem == "Sundae m. chokoladesauce" || (String)listBox.SelectedItem == "McDonut sukkerovertræk" || (String)listBox.SelectedItem == "McFlurry Daim" || (String)listBox.SelectedItem == "McFlurry Smarties" || (String)listBox.SelectedItem == "Sundae m. jordbærdsauce" || (String)listBox.SelectedItem == "Sundae m. karamelsauce" || (String)listBox.SelectedItem == "Triple chokolade muffin" || (String)listBox.SelectedItem == "Vaffelis")
                {
                    comboBox3.Items.Add(listBox.SelectedItem);
                    listBox.Items.Remove(listBox.SelectedItem);
                }
                else if (listBox.SelectedItem.ToString().Contains("Stor") || listBox.SelectedItem.ToString().Contains("Mellem") || listBox.SelectedItem.ToString().Contains("Lille") || listBox.SelectedItem.ToString().Contains("9") || listBox.SelectedItem.ToString().Contains("6") || listBox.SelectedItem.ToString().Contains("4"))
                {
                    string objectToString = listBox.SelectedItem.ToString();
                    string[] ord = objektToString.Split(' ');
                    string selectedItem = listBox.SelectedItem;

                    var check = comboBox.Items.Cast<string>()
                         .ToList()
                         .FirstOrDefault(c => c.Contains(selectedItem));
                    if (check != null)
                    {
                        comboBox.Items.Add("Chicken McNuggets");
                    }
                    else
                    {
                        listBox.Items.Remove(selectedItem);
                    }
                    if (listBox.SelectedItem.ToString().Contains("Pommes Frites"))
                        comboBox2.Items.Add(ord[1] + " " + ord[2]);
                    else if (listBox.SelectedItem.ToString().Equals("Stor Coca-Cola") || listBox.SelectedItem.ToString().Equals("Mellem Coca-Cola") || listBox.SelectedItem.ToString().Equals("Lille Coca-Cola"))
                        comboBox4.Items.Add(ord[1]);
                    else if (listBox.SelectedItem.ToString().Contains("Milkshake"))
                        comboBox4.Items.Add(ord[1] + " " + ord[2] + " " + ord[3]);
                    else
                        comboBox4.Items.Add(ord[1] + " " + ord[2]);
                    listBox.Items.Remove(listBox.SelectedItem);
                }
            }
            catch
            {
                MessageBox.Show(// Message);
            }
        }

1 个答案:

答案 0 :(得分:0)

我们需要Cast string 来自ComboBox的项目,以便我们现在可以轻松查看已经{{1}的ListBox.SelectedItem }}

ComboBox.Items