简体   繁体   中英

C# Trying to use a selected value from the combo box

So the rest of the code is working fine but when it gets to the part where you have to select from predetermined values from the drop down menu, it doesn't display my options but instead allows the user to choose their own.

namespace A2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Add("15%");
            comboBox1.Items.Add("18%");
            comboBox1.Items.Add("20%");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double amount, tax, tip, total, final;
            amount = double.Parse(textBox1.Text);
            tax = amount * 0.1;
            labelTax.Text = tax.ToString();
            total = amount + tax;
            labelTotal.Text = total.ToString();
            tip = amount * double.Parse(comboBox1.Text);
            labelTip.Text = tip.ToString();
            final = amount + tip + tax;
            labelFinal.Text = final.ToString();
        }
    }
}

The SelectedIndexChanged event is fired when the user changes a selection in the combo box. With your code above, any time they choose a new item, you're adding 3 more.

However, as it stands now, the combo box is empty when the form loads, so this event will never fire (there are no choices to select). And then, as you mention, the user can type something in themselves.

To fix this, you might want to move that code into the Form_Load event instead. This way, the combo box will have the items added from the start. You also might want to set the DropDownStyle to DropDownList , which will prevent the user from being able to type in the combo box. And then you can set the SelectedIndex to 0 so the first item is selected:

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.Items.Add("15%");
    comboBox1.Items.Add("18%");
    comboBox1.Items.Add("20%");
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    comboBox1.SelectedIndex = 0;
}

Another problem you will run into is having the percent sign as part of the comboBox item text. double.Parse() will not know what to do with this. In order to resolve this, you can use the string.Replace() method to replace the percent character with an empty string before parsing as a double.

Additionally, you will want to divide this whole number by 100 so it's treated as a percentage:

tip = amount * (double.Parse(comboBox1.Text.Replace("%", "")) / 100);

Another approach to use data-binding and have strong typed values.
Use decimal as type for money related calculations

public class Tax
{
    public decimal Value { get; }
    public string Text { get => Value.ToString("0.## '%'"); }

    public Tax(decimal value)
    {
        Value = value;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    var taxes = new[]
    {
        new Tax(15),
        new Tax(18),
        new Tax(20),
    }

    comboBox1.ValueMember = "Value";
    comboBox1.DisplayMember = "Text";
    comboBox1.DataSource = taxes;
    comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
    comboBox1.SelectedValue = 15;
}

Then in button click you can access selected value

var selectedTax = (decimal)combobox.SelectedValue;

or you can get whole selected object

var selectedTax = (Tax)combobox.SelectedItem;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM