-1

I have a comboBox where the user can select from a variety of drinks

comboBoxBeverage.Items.Add("");
comboBoxBeverage.Items.Add("Soda $1.95");
comboBoxBeverage.Items.Add("Tea $1.50");
comboBoxBeverage.Items.Add("Coffee $1.25");
comboBoxBeverage.Items.Add("Mineral Water $2.95");
comboBoxBeverage.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxBeverage.SelectedIndex = 0;

but I want to take the option selected in the combobox and split it so I can use the price.

I tried doing

double beverage;
beverage = double.Parse(comboBoxBeverage.Text.TrimStart(new[] { '$' }));
labelSubtotal.Text = beverage.ToString();

but it's giving me an error:

System.FormatException: 'Input string was not in a correct format.'

xoxoGG
  • 11
  • 4
  • What's the error it's giving you? – Jecoms Oct 29 '17 at 21:18
  • during debugging it's giving me "System.FormatException: 'Input string was not in a correct format.'" – xoxoGG Oct 29 '17 at 21:19
  • I'd use a datasource from the DB or List so I could show `DisplayMember` to the user and get the price from the `ValueMember`. Please read [ask] and take the [tour]. `it's giving me an error` is not the least bit helpful in describing the problem – Ňɏssa Pøngjǣrdenlarp Oct 29 '17 at 21:20
  • 1
    A primitive way would be `var strPrice = text.Split('$').Skip(1).FirstOrDefault()` – L.B Oct 29 '17 at 21:25

2 Answers2

3

A more OOP approach to your question and also removing the problem of parsing the input text is the following:

First create a class Beverage

public class Beverage
{
    public string Description {get;set;}
    public decimal Price {get;set;}
    public override string ToString()
    {
       return $"{this.Description} {(this.Price != 0 ? this.Price.ToString("C") : "")}";
    }
}

now create a List<Beverage> with your data

List<Beverage> drinks = new List<Beverage>()
{
    new Beverage {Description = "", Price = 0m},
    new Beverage {Description = "Soda", Price = 1.95m},
    new Beverage {Description = "Tea", Price = 1.50m},
    new Beverage {Description = "Coffee", Price = 1.25m},
    new Beverage {Description = "Mineral Water", Price = 2.95m}
};
comboBoxBeverage.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxBeverage.DataSource = drinks;

Now you can retrieve a Beverage instance from your combobox instead of a string and you get the Price directly from this Beverage instance

private void Button1_Click(object sender, EventArgs e)
{                     
    if(comboBoxBeverage.SelectedItem != null)
    {
        Beverage b = comboBoxBeverage.SelectedItem as Beverage;
        SubTotal += b.Price;
        labelSubtotal.Text = SubTotal.ToString("C");
    }
}

This solution works because the ComboBox calls ToString() for every item added to its list through the DataSource property unless you set the DisplayMember and ValueMember properties. Also notice that when dealing with currency values you should use the decimal type, not the double type

Steve
  • 213,761
  • 22
  • 232
  • 286
0

You might give this a shot. It just takes the selected item, splits it into two elements where the $ is located, skips over the first element and assigns the second element's value to itemPrice:

using System;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public double SubTotal { get; set; }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxBeverage.Items.Add("");
            comboBoxBeverage.Items.Add("Soda $1.95");
            comboBoxBeverage.Items.Add("Tea $1.50");
            comboBoxBeverage.Items.Add("Coffee $1.25");
            comboBoxBeverage.Items.Add("Mineral Water $2.95");
            comboBoxBeverage.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxBeverage.SelectedIndex = 0;
        }

        private void Button1_Click(object sender, EventArgs e)
        {                     
            var itemPrice = comboBoxBeverage.Text.Split('$').Skip(1).FirstOrDefault();
            SubTotal += double.Parse(itemPrice);
            labelSubtotal.Text = "$" + SubTotal;
        }
    }
}
Kevin
  • 4,798
  • 19
  • 73
  • 120