2013-01-23 119 views
0

我有一個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); 
      } 
     } 
+0

只需添加'的ToString()''來糾正錯誤,字符串將selectedItem = listBox.SelectedItem.ToString();' – spajce

+0

我想這一點,但現在不會添加 「雞McNugguts」 來我的組合框。 –

+0

它應該喜歡我的最終答案。請嘗試。 – spajce

回答

0

我們需要CaststringComboBox的項目,所以我們現在可以輕鬆地檢查ListBox.SelectedItem已經從ComboBox.Items

private void buttonRemove_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      string selectedItems = listBox1.SelectedItem.ToString(); 

      var check = comboBox1.Items.Cast<string>() 
       .ToList() 
       .FirstOrDefault(c => c.Contains(selectedItems)); 

      if (check != null) 
      { 

      } 
      else 
      { 
       comboBox.Items.Add("Chicken McNuggets"); 
       listBox1.Items.Remove(selectedItems); 
      } 
     } 
     catch 
     { 
      //MessageBox.Show(); 
     } 
    } 
+0

感謝您的答覆。我真的不知道如何將它與我自己的代碼放在一起。你能不能就這個代碼和我一起編寫代碼字段? –

+0

好吧,我會嘗試修改,堅持。 – spajce

+0

@KimBrunJørgensen,我更新了我的答案,我希望它可以幫助你:) – spajce