将项目添加到具有多个值的组合框C#

将项目添加到具有多个值的组合框C#,c#,winforms,combobox,C#,Winforms,Combobox,目前我有一个包含三个硬编码项的组合框。 每个项目包含2个值。我使用switch case语句根据所选项目获取每个项目的值 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;

目前我有一个包含三个硬编码项的组合框。 每个项目包含2个值。我使用switch case语句根据所选项目获取每个项目的值

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值向组合框中添加更多项。我如何能够动态添加case语句并在每个case条件下定义值?我已经研究过使用datatable,但是我不知道当选择一个项目时如何从datatable中获取多个valuemembers

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....
}

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

您可以使用数据源在组合框上使用绑定。组合框还可以绑定到除基本值(string/int/hardcoded值)之外的其他对象。因此,您可以创建一个小类来表示您在switch语句中设置的值,然后使用DisplayMember来说明哪个属性应该在组合框中可见

这样一个基本类的例子可以是

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

    public int B { get; set; }

    public string Title { get; set; }
}
由于您谈论的是用户动态地向组合框添加值,因此可以使用包含单独类的BindingList,该BindingList可以是类内的受保护字段,当用户添加新数据结构时,您可以向其中添加新数据结构,然后用添加的新值自动更新组合框

组合框的设置可以在Form_Load或Form构造函数(在InitializeComponent()调用之后)中完成,如下所示:

// 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;
    }
}
//您的表单
公共部分类Form1:Form
{
//属性包含将在组合框中显示的所有项
受保护的IList dataItems=new BindingList();
//一种保留所选引用的方法,您不必总是询问组合框,它会在选择更改事件时更新
受保护的数据结构selectedDataStructure=null;
公共表格1()
{
初始化组件();
//在此处创建默认值
Add(新的数据结构{A=0.5,B=100,Title=“Some value”});
添加(新的数据结构{A=0.75,B=100,Title=“More value”});
Add(新的数据结构{A=0.95,B=100,Title=“甚至更多的值”});
//将数据项分配给combobox数据源
comboBox1.DataSource=数据项;
//说出组合框应在下拉列表中显示的内容
comboBox1.DisplayMember=“Title”;
//将其设置为仅列表,不键入
comboBox1.DropDownStyle=ComboBoxStyle.DropDownList;
//注册到每次选择更改时触发的事件
comboBox1.SelectedIndexChanged+=Combox1\u SelectedIndexChanged;
}
//将项添加到dataItems的方法(并且由于BindingContext而自动添加到ComboBox)
私有void Add(双a,整数b,字符串标题)
{
Add(新的数据结构{A=A,B=B,Title=Title});
}
//值更改时,更新selectedDataStructure字段
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
ComboBox combo=发件人作为ComboBox;
if(combo==null)
{
返回;
}
selectedDataStructure=combo.SelectedItem作为数据结构;
如果(selectedDataStructure==null)
{
Show(“您当时没有选择任何内容”);
}
其他的
{
Show(string.Format(“您当前选择了{0},A={1:n2},B={2}”,selectedDataStructure.Title,selectedDataStructure.A,selectedDataStructure.B));
}
}
//要在按钮上添加项目,请单击
私有void AddComboBoxItemButton_单击(对象发送者,事件参数e)
{
字符串标题=textBox1.Text;
if(string.IsNullOrWhiteSpace(title))
{
Show(“需要标题!”);
返回;
}
随机=新随机();
double a=random.NextDouble();
int b=random.Next();
添加(a、b、标题);
textBox1.Text=string.Empty;
}
}
这样,您就可以随时获得所选项目,您可以从所选项目的属性中请求值,而不必担心将组合框与当前可见的项目同步,这些项目来自:

尽管组合框通常用于显示文本项,但您可以向组合框添加任何对象。通常,组合框中对象的表示形式是该对象的ToString方法返回的字符串。如果希望显示对象的成员,请通过将DisplayMember属性设置为相应成员的名称来选择将显示的成员。您还可以通过设置ValueMember属性来选择对象的成员,该成员将表示对象返回的值。有关更多信息,请参阅ListControl


因此,您只需将包含所有信息的对象直接添加到组合框的
集合中即可。稍后,检索SelectedItem属性并将其转换回正确的类型。

将值存储在类中,制作列表框的数据源
列表,然后计算
myList[SelectedIndex]
以获取(或设置)价值观。对于使用基本的
项目
集合可以做的事情来说,这是大量的工作。主要有很多评论:)我认为有更多的东西可以学习从来都不是坏事