2016-09-10 4 views
2

현재 제품 유지 관리 페이지가있는 데스크톱 응용 프로그램을 개발 중이며 모든 메시지 유효성 검사 오류를 단일 메시지 상자에 표시하는 방법을 찾고 있습니다.단일 메시지 상자에 여러 오류 메시지 표시

나는 아래의 코드를 사용하여 유효성 검사 오류 당 하나의 메시지 상자를 표시하고 (검증은 저장 버튼 바인딩)

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

나는 사용자에게 모든 오류를 알 용이성을 제공하고 싶습니다 하나의 메시지 상자 당 하나씩 알기보다는 한 번에.

미리 감사드립니다. :)

답변

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

괜 찮 아 요 그것을 시도, 감사합니다! –

관련 문제