2016-09-10 17 views
2

現在、製品保守ページを持つデスクトップアプリケーションを開発中です。すべての検証エラーを1つのメッセージボックスに表示する方法を探しています。複数のエラーメッセージを1つのメッセージボックスに表示

私は以下のコードを使用して検証エラーごとに1つのメッセージボックスを表示しています:(検証は、保存ボタンにバインドされている)

 if ((Convert.ToInt32(txtQuantity.Text)) > 20000) 
     { 
      MessageBox.Show("Maximum quantity is 20,000!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      txtQuantity.Focus(); 
      return; 
     } 

     if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text))) 
     { 
      MessageBox.Show("Quantity is lower than Critical Level.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      txtQuantity.Focus(); 
      return; 
     } 

     if (txtCriticalLevel.Text == "0") 
     { 
      MessageBox.Show("Please check for zero values!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      txtCriticalLevel.Focus(); 
      return; 
     } 

私は、ユーザーにすべてのエラーを知るの容易さを与えるしたいのですが表示されたメッセージボックスごとに1つずつ知っているのとは対照的に、

ありがとうございます! :)

答えて

0

迅速な汚いソリューションは次のようになります。答えを

string errorMessages = String.Empty; 

    if ((Convert.ToInt32(txtQuantity.Text)) > 20000) 
    { 
     errorMessages +="- Maximum quantity is 20,000!\r\n"; 
     txtQuantity.Focus(); 
     return; 
    } 

    if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text))) 
    { 
     errorMessages += "- Quantity is lower than Critical Level.\r\n"; 
     txtQuantity.Focus(); 
     return; 
    } 

    if (txtCriticalLevel.Text == "0") 
    { 
     errorMessages += "- Please check for zero values!\r\n"; 
     txtCriticalLevel.Focus(); 
     return; 
    } 

    if(!String.IsNullOrEmpty(errorMessages)) 
     MessageBox.Show(errorMessages, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
+0

これを試してみます、ありがとう!!! –

+0

concat文字列がStringBuilderを使用する方が良い場合https://msdn.microsoft.com/en-us/library/ms228504.aspx –

+0

コードが不思議でした!どうもありがとうございます!!なぜあなたはその「汚い」の理由を説明できますか? –

1

あなたはStringBuilderのを使用して、それにエラーを追加することができます。

StringBuilder sb = new StringBuilder(); 


if ((Convert.ToInt32(txtQuantity.Text)) > 20000) 
     { 
       sb.AppendLine("Maximum quantity is 20,000!");    
     } 



if ((Convert.ToInt32(txtQuantity.Text)) <= (Convert.ToInt32(txtCriticalLevel.Text))) 
    { 
     sb.AppendLine("Quantity is lower than Critical Level."); 
    } 

.... 
    MessageBox.Show(sb.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
+0

感謝!私はこれを見ていきますが、どのように検証するのでしょうか?私に可能なサンプルを見せてくれますか? :) –

+0

@RonReyes私の答えを更新;) –

+0

大丈夫、それを試してみる、ありがとう! –

関連する問題