-1

Basically refreshing some C# knowledge making a converter; I'm trying to make a window pop up when no numbers are in the textbox but when I click the button I get this error:

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

private void btnConvert_Click(object sender, EventArgs e)
    {

        // Killogram convertion

        double i = double.Parse(txtboxAmount.Text);
        if (comboBoxFrom.SelectedItem == "KG" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i * 2.20462262185;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.2046;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i * 6.35;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        // Pound convertion


        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "ST")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.205;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        // Stone convertion

        if (comboBoxFrom.SelectedItem == "ST" && comboBoxTo.SelectedItem == "LB")
        {
            double conver = i * 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "ST")
        {
            double conver = i / 14;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        if (comboBoxFrom.SelectedItem == "LB" && comboBoxTo.SelectedItem == "KG")
        {
            double conver = i / 2.205;
            double converdec = Math.Round((Double)conver, 2);
            txtBoxResult.Text = "Converted Amount : " + converdec;
        }

        else if (comboBoxFrom.SelectedItem == null && comboBoxTo.SelectedItem == null)
        {
            MessageBox.Show("Enter a valid weight amount and/or select a unit of measurement.");
        }

    }

The line causing the issues is below I string turning it into a string with to string but no luck

double i = double.Parse(txtboxAmount.Text);
d219
  • 2,707
  • 5
  • 31
  • 36
inFlux
  • 45
  • 5

1 Answers1

0

Use TryParse to fail gracefully:

var text = "123.4";

double result;

if (double.TryParse(text, out result))
{
    // do something
}

The other thing to account for might be the culture, if you use a French system and input English notation,

i.e. . instead of , it will fail parsing your input.

if (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out result))
{
    // do something
}
aybe
  • 15,516
  • 9
  • 57
  • 105