将具有多个值的项目添加到组合框C# [英] Add items to combobox with multiple values C#

查看:48
本文介绍了将具有多个值的项目添加到组合框C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个带有三个硬编码项目的组合框.每个项目带有2个值.我正在使用switch case语句来获取每个项目的值,具体取决于所选的项目.

currently I have a combobox with three hard coded items. Each item carries 2 values. I'm using a switch case statement to get the values for each item depending on which item is selected.

Switch(combobox.selectedindex)
{
    case 0: // Item 1 in combobox
        a = 100;
        b = 0.1;
        break;
    case 1: // Item 2 in combobox
        a = 300;
        b = 0.5;
        break;
    //and so on....
}

我正在尝试添加一项功能,以允许用户使用输入的a和b值将更多项目添加到组合框中.我如何能够动态添加案例陈述并在每种案例条件下定义值?我有一个看一个使用数据表来代替,但我不知道什么时候是选择了一个项目如何获得多个valuemembers了DataTable的.

I'm trying to add a feature to allow the user to add more items into the combobox with inputted a and b values. How would i be able to dynamically add case statements and define the values under each case condition? I've had a look at using a datatable instead but I don't know how to get multiple valuemembers out of the datatable when one item is selected.

此外,我想将用户添加的项目及其对应的值保存到.dat文件中.因此,当重新打开程序时,它将能够从文件中加载用户添加的项目列表.我考虑过为此使用streamwriter和readline,但不确定如何实现.

Also, I would like to save the user added items and it's corresponding values to a .dat file. So when the program is re-opened it will be able to load the list of items added by the user from the file. I considered using streamwriter and readline for this but I'm unsure how it would be done.

推荐答案

您可以使用数据源在组合框上使用绑定.ComboBox还可以绑定到除原始值(字符串/整数/硬编码值)以外的其他内容.因此,您可以创建一个小类来表示您在switch语句中设置的值,然后使用DisplayMember来说出哪个属性在组合框中可见.

You can use Binding on a combobox using the DataSource. The ComboBox can also be bound to other things than Primitive values (string/int/hardcoded values). So you could make a small class that represents the values you are setting in your switch statement, and then use the DisplayMember to say which property should be visible in the combobox.

此类基本类的示例可能是

An example of such a basic class could be

public class DataStructure
{
    public double A { get; set; }

    public int B { get; set; }

    public string Title { get; set; }
}

由于您是在谈论用户向组合框动态添加值,因此可以使用包含单独类的BindingList,此BindingList可以是类内部的受保护字段,当用户添加一个新的DataStructure时,您可以在其中添加新的DataStructure,然后使用您添加的新值自动更新组合框.

Since you are talking about users adding values to the combobox dynamically, you could use a BindingList that contains the separate classes, this BindingList could be a protected field inside your class, to which you add the new DataStructure when the user adds one, and then automatically updates the combobox with the new value you added.

可以在Form_Load或Form构造函数中(在InitializeComponent()调用之后)完成ComboBox的设置,例如:

The setup of the ComboBox, can be done in either Form_Load, or in the Form Constructor (after the InitializeComponent() call), like such:

// your form
public partial class Form1 : Form
{
    // the property contains all the items that will be shown in the combobox
    protected IList<DataStructure> dataItems = new BindingList<DataStructure>();
    // a way to keep the selected reference that you do not always have to ask the combobox, gets updated on selection changed events
    protected DataStructure selectedDataStructure = null;

    public Form1()
    {
        InitializeComponent();
        // create your default values here
        dataItems.Add(new DataStructure { A = 0.5, B = 100, Title = "Some value" });
        dataItems.Add(new DataStructure { A = 0.75, B = 100, Title = "More value" });
        dataItems.Add(new DataStructure { A = 0.95, B = 100, Title = "Even more value" });
        // assign the dataitems to the combobox datasource
        comboBox1.DataSource = dataItems;
        // Say what the combobox should show in the dropdown
        comboBox1.DisplayMember = "Title";
        // set it to list only, no typing
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        // register to the event that triggers each time the selection changes
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
    }

    // a method to add items to the dataItems (and automatically to the ComboBox thanks to the BindingContext)
    private void Add(double a, int b, string title)
    {
        dataItems.Add(new DataStructure { A = a, B = b, Title = title });
    }

    // when the value changes, update the selectedDataStructure field
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox combo = sender as ComboBox;
        if (combo == null)
        {
            return;
        }
        selectedDataStructure = combo.SelectedItem as DataStructure;
        if (selectedDataStructure == null)
        {
            MessageBox.Show("You didn't select anything at the moment");
        }
        else
        {
            MessageBox.Show(string.Format("You currently selected {0} with A = {1:n2}, B = {2}", selectedDataStructure.Title, selectedDataStructure.A, selectedDataStructure.B));
        }
    }

    // to add items on button click
    private void AddComboBoxItemButton_Click(object sender, EventArgs e)
    {
        string title = textBox1.Text;
        if (string.IsNullOrWhiteSpace(title))
        {
            MessageBox.Show("A title is required!");
            return;
        }
        Random random = new Random();
        double a = random.NextDouble();
        int b = random.Next();
        Add(a, b, title);
        textBox1.Text = string.Empty;
    }
}

像这样,您总是可以随时使用选定的项目,可以从选定的属性中请求值,而不必担心将ComboBox与当前可见的项目进行同步

Like this, you have the selected item always at hand, you can request the values from the properties of the selected, and you don't have to worry about syncing the ComboBox with the items currently visible

这篇关于将具有多个值的项目添加到组合框C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆