2017-04-26 7 views
-1

小さなプログラムを作成していますが、エラーが発生します。"System.Format.Exception"に関するコードにエラーがあります

System.Format.Exception

私が最後の行を追加した後、それはよかった:
- ユーザーが任意の価格を入力しない場合は、メッセージボックスにエラーが表示されます。

ここ
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);  
    } 
} 

エラーとスクリーンショットおよびデバッグ値:

enter link description here

+0

私もきちんとそれを読み取ることができませんし、私はそれはあなたがフロートをイマイチフロートに何かを変換していることがわかります。あなたはそれを使って何かをしようとする前に入力を確認してください – EpicKip

+1

'text_entrer.Text'の値は何ですか? –

+0

これは、ユーザーが価格を追加する最初のテキストゾーンです。 –

答えて

0

例外の直接の理由は、float(たとえば、bla-bla-bla - は有効な浮動小数点値ではありません)に解析できない文字列を含んでいることです。text_entrer.Text私は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) 
    ... 
+0

ありがとう兄弟それは仕事です –

0

どうやら、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; 
} 
関連する問題