2017-09-17 23 views
1

ここに私のコードです。ユーザーが「計算」ボタンをクリックすると、コードがそれを実行します。ただし、ユーザーが数値を入力しなかった場合、例外がスローされ、エラーメッセージがポップアップします。私は私のエラーメッセージが「あなたは番号を忘れてしまった!」と言ってはいけません。 「入力文字列が正しい形式でない」という自動メッセージがポップアップします。エラーメッセージを変更するには?Visual Studio C#例外エラーメッセージ

 private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      // Local variables 

      String num1; 
      String num2; 
      double number1; 
      double number2; 
      double result; 

      // Get the numbers from the textboxes 

      num1 = txtInput1.Text; 
      num2 = txtInput2.Text; 
      number1 = double.Parse(num1); 
      number2 = double.Parse(num2); 

      // Determine the user clicks the Addition radiobutton 
      if (rdbAdd.Checked) 
      { 
       // Add two numbers 
       result = number1 + number2; 
      } 
      else 
      { 
       // Determine the user clicks the Subtraction radiobutton 
       if (rdbSubtract.Checked) 
       { 
        // Subtract numbers 
        result = number1 - number2; 
       } 
       else 
       { 
        // Determine the user clicks the Multiply radiobutton 
        if (rdbMultiply.Checked) 
        { 
         // Multiply numbers 
         result = number1 * number2; 
        } 
        else 
        { 
         // Devide numbers when the user clicks the Devision radiobutton 
         result = number1/number2; 
        } 
       } 
      } 

      // Display the result 

      txtDisplay.Text = result.ToString(); 
     } 
     catch (Exception ex) 
     { 
      // Display an error message 

      MessageBox.Show(ex.Message); 
     } 

    } 
+2

「あなたはそれを間違ったダミー」というメッセージで顔に叩くことはありません。実際にあなたが間違っていたのです。ユーザーが何かを入力するまでボタンを有効にしないでください。 TextChangedイベントを使用します。 ErrorProviderを使用すると、正しい結果を得るのに役立ちます。 –

答えて

1

メッセージの選択を表示するには...

MessageBox.Show("Your message goes here.") 

例外は、それは自身のメッセージのしている、あなたは、あなたが興味を持っている例外の種類を傍受し、例外にapproriateあなたのメッセージが表示されます。テキストフィールドに何もない場合は、Double.Parseは、例外がスローされます(それがスローする例外のDouble.Parseを見て)

しかしnumber2のはゼロであり、ユーザーは「分割」を選択した場合、あなたは(別の例外が発生しますゼロで割る)。

一般的に、あなたの入力を検証し、単にDouble.Parseを使用するだけで十分です。しかし、通常、あなたはもっと必要があります。また、アプリケーションを国際化する場合は、ロケールごとに解析する必要があります。上のリンクを参照してください。ローカライズされた解析の方法があります。

1

これは、この例外のデフォルトのメッセージで、FormatExceptionです。

あなたは例外のようなものをキャッチして、ちょうどあなた自身のメッセージを表示することができます。

try 
    { 
    .... your code ... 
    } 
    catch (FormatException ex) 
    { 
     //FormatException was thrown, display your message 
     MessageBox.Show("You forgot to put the number!"); 
    } 
    catch (Exception ex) 
    { 
     // Some other kind of exception was thrown ... 
     MessageBox.Show(ex.Message); 
    } 
1
あなたが処理したいいくつかの「キャッチ」の句は、例外の種類ごとに1つのことができ

try 
{ 
    // Your code goes here 
} 
catch (DivideByZeroException ex) 
{ 
    MessageBox.Show("Cannot divide by zero! " + ex.Message); 
} 
catch (Exception ex) 
{ 
    // This is a generic exception 
    MessageBox.Show("Error: " + ex.Message); 
} 

より具体的なものからより一般的なものへと注文する必要があります。

1

これを試しても、txtInput1とtxtInput2のいずれかがnullまたは空の場合は処理を続行できません。

if(string.IsNullOrWhiteSpace(txtInput1.Text) == true) 
{ 
    MessageBox.Show("Your message goes here."); 
    return; // this is important, return if true 
} 

if(string.IsNullOrWhiteSpace(txtInput2.Text) == true) 
{ 
    MessageBox.Show("Your message goes here."); 
    return; // this is important, return if true 
} 

      // then 
. . . . . // proceed if no problem found 
関連する問題