Как удалить дубликаты ключей в разных combobox?
Я использую пару ключ-значение, чтобы сформулировать 4 разных comboBox, но один и тот же ключ будет отображаться из предыдущего comboBox в новый comboBox. Я новичок в windows form, поэтому не уверен, что это также проблема привязки источника данных.
Что я уже пробовал:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BillCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Create a List to store our KeyValuePairs List<KeyValuePair<string, Decimal>> data = new List<KeyValuePair<string, Decimal>>(); //**BEVERAGE** data.Add(new KeyValuePair<string, Decimal>("Soda", 1.95m)); data.Add(new KeyValuePair<string, Decimal>("Tea", 1.50m)); data.Add(new KeyValuePair<string, Decimal>("Coffee", 1.25m)); data.Add(new KeyValuePair<string, Decimal>("Mineral Water", 2.95m)); data.Add(new KeyValuePair<string, Decimal>("Juice", 2.50m)); data.Add(new KeyValuePair<string, Decimal>("Milk", 1.50m)); // Clear the combobox beverageBox.DataSource = null; beverageBox.Items.Clear(); // Bind the combobox beverageBox.DataSource = new BindingSource(data, null); beverageBox.DisplayMember = "Key"; beverageBox.ValueMember = "Value"; //**APPETIZER** { appetizerBox.BindingContext = new BindingContext(); data.Add(new KeyValuePair<string, Decimal>("Buffalo Wings", 5.95m)); data.Add(new KeyValuePair<string, Decimal>("Buffalo Fingers", 6.95m)); data.Add(new KeyValuePair<string, Decimal>("Potato Skins", 8.95m)); data.Add(new KeyValuePair<string, Decimal>("Nachos", 8.95m)); data.Add(new KeyValuePair<string, Decimal>("Mushroom Caps", 10.95m)); data.Add(new KeyValuePair<string, Decimal>("Shrimp Cocktail", 12.95m)); data.Add(new KeyValuePair<string, Decimal>("Chips and Sala", 6.96m)); // Clear the combobox appetizerBox.DataSource = null; appetizerBox.Items.Clear(); // Bind the combobox appetizerBox.DataSource = new BindingSource(data, null); appetizerBox.DisplayMember = "Key"; appetizerBox.ValueMember = "Value"; } //**MAIN COURSE** { mainCBox.BindingContext = new BindingContext(); data.Add(new KeyValuePair<string, Decimal>("Chicken Alfredo", 13.95m)); data.Add(new KeyValuePair<string, Decimal>("Chicken Picatta", 13.95m)); data.Add(new KeyValuePair<string, Decimal>("Turkey Club", 11.95m)); data.Add(new KeyValuePair<string, Decimal>("Lobster Pie", 19.95m)); data.Add(new KeyValuePair<string, Decimal>("Prime Rib", 20.95m)); data.Add(new KeyValuePair<string, Decimal>("Shrimp Scampi", 18.95m)); data.Add(new KeyValuePair<string, Decimal>("Turkey Dinner", 13.96m)); data.Add(new KeyValuePair<string, Decimal>("Stuffed Chicken", 14.96m)); data.Add(new KeyValuePair<string, Decimal>("Seafood Alfredo", 15.96m)); // Clear the combobox mainCBox.DataSource = null; mainCBox.Items.Clear(); // Bind the combobox mainCBox.DataSource = new BindingSource(data, null); mainCBox.DisplayMember = "Key"; mainCBox.ValueMember = "Value"; } //**DESSERT** { dessertBox.BindingContext = new BindingContext(); data.Add(new KeyValuePair<string, Decimal>("Apple Pie", 5.95m)); data.Add(new KeyValuePair<string, Decimal>("Sundae", 3.95m)); data.Add(new KeyValuePair<string, Decimal>("Carrot Cake", 5.95m)); data.Add(new KeyValuePair<string, Decimal>("Mud Pie", 4.95m)); data.Add(new KeyValuePair<string, Decimal>("Apple Crisp", 5.95m)); // Clear the combobox dessertBox.DataSource = null; dessertBox.Items.Clear(); // Bind the combobox dessertBox.DataSource = new BindingSource(data, null); dessertBox.DisplayMember = "Key"; dessertBox.ValueMember = "Value"; } } private void Appetizer_Click(object sender, EventArgs e) { } private void beverageBox_SelectedIndexChanged(object sender, EventArgs e) { // Get the selected item in the combobox KeyValuePair<string, Decimal> selectedPair = (KeyValuePair<string, Decimal>)beverageBox.SelectedItem; // Show selected information on screen lblSelectedKey.Text = selectedPair.ToString(); } private void appetizerBox_SelectedIndexChanged(object sender, EventArgs e) { // Get the selected item in the combobox KeyValuePair<string, Decimal> selectedPair = (KeyValuePair<string, Decimal>)appetizerBox.SelectedItem; // Show selected information on screen lblSelectedKey2.Text = selectedPair.ToString(); } private void mainCBox_SelectedIndexChanged(object sender, EventArgs e) { // Get the selected item in the combobox KeyValuePair<string, Decimal> selectedPair = (KeyValuePair<string, Decimal>)mainCBox.SelectedItem; // Show selected information on screen lblSelectedKey3.Text = selectedPair.ToString(); } private void dessertBox_SelectedIndexChanged(object sender, EventArgs e) { // Get the selected item in the combobox KeyValuePair<string, Decimal> selectedPair = (KeyValuePair<string, Decimal>)dessertBox.SelectedItem; // Show selected information on screen lblSelectedKey4.Text = selectedPair.ToString(); } } }