【问题标题】:I have a error in my code about "System.Format.Exception"我的代码中有关于“System.Format.Exception”的错误
【发布时间】:2017-04-26 12:31:32
【问题描述】:

我正在创建一个小程序,但它会引发错误。

System.Format.Exception

在我添加最后一行之后很好:
- 如果用户没有输入任何价格,则会显示 MessageBox 错误。

private void button2_Click(object sender, EventArgs e)
{
    float mont,ope,mont_ht;
    mont = float.Parse(text_entrer.Text); // ERROR HERE : 'System.Format.Exception'
    if (radioButton4.Checked)
    { 
        text_resultat.Text = mont.ToString(); 
    }
    else if(radioButton5.Checked && radioButton1.Checked)
    {
        ope = mont * 20 / 100;
        mont_ht = mont + ope;
        text_resultat.Text = mont_ht.ToString();
    }
    else if (radioButton5.Checked && radioButton2.Checked)
    {
        ope = mont * 12 / 100;
        mont_ht = mont + ope;
        text_resultat.Text = mont_ht.ToString();
    }
    else if (radioButton5.Checked && radioButton3.Checked)
    {
        ope = mont * 5 / 100;
        mont_ht = mont + ope;
        text_resultat.Text = mont_ht.ToString();
    }

    if (String.IsNullOrEmpty(text_entrer.Text))
    {
        MessageBox.Show("no montant","EROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);     
    }
}

这里是错误和调试值的截图:

【问题讨论】:

  • 我什至无法正确阅读,我可以说这意味着您正在将某些东西转换为不是浮点数的浮点数。在尝试使用它之前检查输入
  • text_entrer.Text 的值是多少?
  • 这是用户添加价格的第一个文本区域
  • 该死的,当答案在帖子中时,我问了一个问题。代码块太长,我正在编辑你的问题,使它只包含有用的信息。所以其他人不要迷路。

标签: c# forms winforms


【解决方案1】:

异常的直接原因是text_entrer.Text 包含无法解析为float 的字符串(例如bla-bla-bla - 不是有效的浮点值)。我建议使用TryParse 而不是Parse

   using System.Globalization;

   ...
   float mont,ope,mont_ht;

   if (!float.TryParse(text_entrer.Text, 
                       NumberStyles.Any, 
                       CultureInfo.InvariantCulture, // or CultureInfo.CurrentCulture 
                       out mont) {
     if (text_entrer.CanFocus)
       text_entrer.Focus();

     MessageBox.Show($"{text_entrer.Text} is not a valid floating point value", 
                       Application.ProductName, 
                       MessageBoxButtons.OK, 
                       MessageBoxIcon.Warning);

     return;
   }
   ...
   if (radioButton4.Checked) 
     ...

【讨论】:

    【解决方案2】:

    显然,text_entrer.Text 中的空字符串(即“用户不要输入任何价格”)不是有效的数值,应在尝试进行任何转换之前(即在float.Parse)。所以将你的值检查移到方法的开头,如果检查失败则退出方法:

    private void button2_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(text_entrer.Text))
        {
            MessageBox.Show("no montant","EROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }
        float mont,ope,mont_ht;
        mont = float.Parse(text_entrer.Text); //I HAVE THE PROBLEM HERE
        //..............
    }
    

    此外,您最好使用float.TryParse 防止任何无效的非数字输入:

    if (!float.TryParse(text_entrer.Text, out mont))
    {
        MessageBox.Show("Montant invalide","EROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多