C#Windows窗体-如何为组合框字符串指定一个双精度值,然后将其显示在文本框中?

C#Windows窗体-如何为组合框字符串指定一个双精度值,然后将其显示在文本框中?,c#,windows,forms,combobox,textbox,C#,Windows,Forms,Combobox,Textbox,我一直很难找到一个解决方案来解决我的编码问题,即给组合框项目分配一个双值,然后显示该双值并将其添加到文本框中 例如,饮料组合框包含:苏打水、茶、咖啡、牛奶、果汁等。因此,对于“苏打水”,我想将其值设为$1.95,然后显示在“小计:”文本框中。因此,每当在组合框中选择一个项目时,比如苏打水、果汁和咖啡,每次选择一个项目时,它都会添加到文本框中以前的小计值中 对不起,如果不是太清楚,但任何帮助都将是巨大的,我已经昏昏欲睡,一直试图想出一个解决方案,现在只是挣扎。谢谢大家! 如果我正确理解了您的问题(

我一直很难找到一个解决方案来解决我的编码问题,即给组合框项目分配一个双值,然后显示该双值并将其添加到文本框中

例如,饮料组合框包含:苏打水、茶、咖啡、牛奶、果汁等。因此,对于“苏打水”,我想将其值设为$1.95,然后显示在“小计:”文本框中。因此,每当在组合框中选择一个项目时,比如苏打水、果汁和咖啡,每次选择一个项目时,它都会添加到文本框中以前的小计值中


对不起,如果不是太清楚,但任何帮助都将是巨大的,我已经昏昏欲睡,一直试图想出一个解决方案,现在只是挣扎。谢谢大家!

如果我正确理解了您的问题(请提供您下次已经尝试过的内容,这会使我们更容易提供帮助。)您可以使用字典将您的饮料映射到价格

var priceMapping = new Dictionary<string, double>()
{
    { "Soda", 1.75 },
    { "Tea", 2.00 },
    { "Coffee", 1.50 }
    //etc...
};

如果我正确理解您的问题(请提供您下次已经尝试过的内容,这样我们可以更容易地提供帮助。)您可以使用字典将您的饮料映射到价格

var priceMapping = new Dictionary<string, double>()
{
    { "Soda", 1.75 },
    { "Tea", 2.00 },
    { "Coffee", 1.50 }
    //etc...
};

这将是自定义类型的完美使用。在本例中,您可以创建一个具有Description属性和Price属性的Drink类。重写.ToString()方法以仅显示描述

class Drink
{
    public string Description { get; set; }
    public decimal Price { get; set; }

    public override string ToString()
    {
        return Description;
    }
}
然后,您将实例化一种新饮料,填充描述和价格,并将该对象添加到您的组合框中。组合框将显示“Soda”,但对象的Price属性中也包含1.95

private void Form1_Load(object sender, EventArgs e)
{

    //for demonstration purposes, we're creating 3 Drink
    //objects and adding them to the combobox. Normally
    //you would loop through a data source of some sort
    //and populate your combobox with the newly intantiated objects.
    Drink item;

    item = new Drink();
    item.Description = "Soda";
    item.Price = 1.80M;
    comboBox1.Items.Add(item);

    item = new Drink();
    item.Description = "Coffee";
    item.Price = .95M;
    comboBox1.Items.Add(item);

    item = new Drink();
    item.Description = "Tea";
    item.Price = .65M;
    comboBox1.Items.Add(item);

}
由于combobox包含一个对象,因此您需要将selecteditem转换回饮料,以便从选择中访问价格

private void button1_Click(object sender, EventArgs e)
{
    decimal itemPrice;
    //if the textbox is empty or cannot be parsed to a decimal
    //then we cast the combobox1.SelectedItem to a Drink type
    //place that value into the textbox. If, however, it can be
    //parsed to a decimal then we grab that value and add the
    //price of our newly selected combobox item to the price
    //that is currently in the textbox.
    if(decimal.TryParse(textBox1.Text, out itemPrice))
    {
        textBox1.Text = (itemPrice + ((Drink)(comboBox1.SelectedItem)).Price).ToString();
    }
    else
    {
        textBox1.Text = (((Drink)(comboBox1.SelectedItem)).Price).ToString();
    }
}

这将是自定义类型的完美使用。在本例中,您可以创建一个具有Description属性和Price属性的Drink类。重写.ToString()方法以仅显示描述

class Drink
{
    public string Description { get; set; }
    public decimal Price { get; set; }

    public override string ToString()
    {
        return Description;
    }
}
然后,您将实例化一种新饮料,填充描述和价格,并将该对象添加到您的组合框中。组合框将显示“Soda”,但对象的Price属性中也包含1.95

private void Form1_Load(object sender, EventArgs e)
{

    //for demonstration purposes, we're creating 3 Drink
    //objects and adding them to the combobox. Normally
    //you would loop through a data source of some sort
    //and populate your combobox with the newly intantiated objects.
    Drink item;

    item = new Drink();
    item.Description = "Soda";
    item.Price = 1.80M;
    comboBox1.Items.Add(item);

    item = new Drink();
    item.Description = "Coffee";
    item.Price = .95M;
    comboBox1.Items.Add(item);

    item = new Drink();
    item.Description = "Tea";
    item.Price = .65M;
    comboBox1.Items.Add(item);

}
由于combobox包含一个对象,因此您需要将selecteditem转换回饮料,以便从选择中访问价格

private void button1_Click(object sender, EventArgs e)
{
    decimal itemPrice;
    //if the textbox is empty or cannot be parsed to a decimal
    //then we cast the combobox1.SelectedItem to a Drink type
    //place that value into the textbox. If, however, it can be
    //parsed to a decimal then we grab that value and add the
    //price of our newly selected combobox item to the price
    //that is currently in the textbox.
    if(decimal.TryParse(textBox1.Text, out itemPrice))
    {
        textBox1.Text = (itemPrice + ((Drink)(comboBox1.SelectedItem)).Price).ToString();
    }
    else
    {
        textBox1.Text = (((Drink)(comboBox1.SelectedItem)).Price).ToString();
    }
}

欢迎您能告诉我们您一直在尝试什么“为组合框项目分配双值的编码问题”吗?这将帮助我们帮助你。祝你好运欢迎您能告诉我们您一直在尝试什么“为组合框项目分配双值的编码问题”吗?这将帮助我们帮助你。祝你好运虽然这是一个很好的解决方案,我会在这种情况下使用。。。对于一个(大概)新程序员来说,这可能有点太多了。虽然这是一个很好的解决方案,我会在这种情况下使用它。。。对于一个(大概)新程序员来说,这可能有点过分。