如何删除不同组合框中的重复键?

编程入门 行业动态 更新时间:2024-10-10 14:29:08
本文介绍了如何删除不同组合框中的重复键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用键值对来制定4个不同的comboBox,但是相同的键将从之前的comboBox显示到新的comboBox中。我是Windows窗口的新手,所以我不确定它是否也是一个数据源绑定问题。 我尝试过:

I'm using key value pair's to formulate 4 different comboBox's but the same key's will show from the previous comboBox into the new comboBox. I'm new to windows form so I'm not sure if it's a data source binding issue as well. What I have tried:

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(); } } }

推荐答案

引用:

但是相同的键将从前一个comboBox显示到新的comboBox。

but the same key's will show from the previous comboBox into the new comboBox.

这是你在代码中要求的。 要么在为下一个组合框添加新项目之前清除列表,要么为每个组合框使用不同的列表。 当你不这样做时了解您的代码正在做什么或为什么它做它做的事情,答案是调试器。 使用调试器来查看您的代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。 调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

That is you asked for it in your code. Either you clear the list before adding new items for next combobox, either you use a different list for each combobox. When you don't understand what your code is doing or why it does what it does, the answer is debugger. Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] Basic Debugging with Visual Studio 2010 - YouTube[^] The debugger is here to show you what your code is doing and your task is to compare with what it should do. There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

更多推荐

如何删除不同组合框中的重复键?

本文发布于:2023-11-29 13:33:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   框中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!