2016-11-02 8 views
0

私はC#を初めて使用しています。私は電卓を作るしようとしているが、私はFormatExceptionを投げています処理されていないformatExcepton

private void operator_click(object sender, EventArgs e) { 
    Button button = (Button) sender; 
    operationperformed = button.Text; 
    result = Double.Parse(textBox1.Text); // <- here I have the exception thrown 

    b = true; 
} 
+4

あなたはどの文字列から 'double'を解析しようとしています「ダブル」を表していません。たとえば、文字列が '' hello ''の場合、それを数値に変換することはできません。それは数字ではないからです。このような状況を処理するために 'double.TryParse()'を見てください。 – David

+0

それは、入力が倍数または数字ではないと言います。入力は何ですか? – Badiparmagi

+0

値を変換するDouble.TryParseメソッドを試してください – tabby

答えて

1

私はdouble.TryParse代わりのdouble.Parseを使用することをお勧め:

private void operator_click(object sender, EventArgs e) { 
    double v; 

    if (!double.TryParse(textBox1.Text, out v)) { 
    // textBox1.Text doesn't contain double, e.g. "bla-bla-bla" 

    //TODO:put a warning/error message here 

    return; 
    } 

    // textBox1.Text has a double value which is v 
    operationperformed = (sender as Button).Text; 
    result = v; 
    b = true; 
} 
関連する問題