未处理的字符串格式不正确

时间:2019-04-06 22:13:50

标签: c# string casting

基本上刷新了一些C#知识,使之成为一个转换器;当textbox中没有数字时,我试图弹出一个窗口,但是当我单击button时,出现此错误:

  

System.FormatException:'输入字符串的格式不正确。'

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.");
        }

    }

导致问题的行在下面,我将其转换为字符串,但没有运气

double i = double.Parse(txtboxAmount.Text);

1 个答案:

答案 0 :(得分:0)

使用TryParse正常失败:

var text = "123.4";

double result;

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

要说明的另一件事可能是文化,如果您使用法语系统并输入英文符号,则

.而不是,,它将无法解析您的输入。

if (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture.NumberFormat, out result))
{
    // do something
}