2016-07-21 1 views
1

모든 항목을 검사하기 위해 C# Windows 양식을 작성하면 모든 항목이 유효 할 때 메시지 상자가 나타나지만 모든 항목이 유효한지 확인하고 메시지 상자를 표시하는 데 도움이 필요합니다. . 아마도이 모든 것을하기가 더 쉬울 것이지만이 모든 것을하는 법을 배울 수도 있습니다. 여기 내가 지금까지 가지고있는 것이있다.C# windows form 모든 항목을 확인한 다음 메시지 상자를 표시합니다.

private void btn_submit_Click(object sender, EventArgs e) 
    { 
     string name = txt_name.Text; 
     string email = txt_email.Text; 
     string address = txt_address.Text; 
     string course = txt_course.Text; 
     string phone = txt_phone.Text; 


     if (name.Length < 8) 
     { 
      txt_name.Text = "Invalid Name"; 
      txt_name.ForeColor = Color.Red; 
     } 
     else 
     { 
      txt_name.ForeColor = Color.Green; 

     } 

     if (email.Contains('@')) 
     { 
      if (email.Contains(".com") || email.Contains(".COM")) 
      { 

       txt_email.ForeColor = Color.Green; 
      } 
      else 
      { 
       txt_email.Text = "invalid Email"; 
       txt_email.ForeColor = Color.Red; 
      } 
     } 
     else 
     { 
      txt_email.Text = "invalid Email"; 
      txt_email.ForeColor = Color.Red; 
     } 

     if (address.Length < 12) 
     { 
      txt_address.Text = "invalid Address"; 
      txt_address.ForeColor = Color.Red; 
     } 
     else 
     { 
      txt_address.ForeColor = Color.Green; 
     } 
     if (course.Contains("Games Design") || course.Contains("Electronics") || course.Contains("Mobile Communications") || course.Contains("GAMES DESIGN") || course.Contains("ELECTRONICS") || course.Contains("MOBILE COMMUNICATIONS")) 
     { 
      txt_course.ForeColor = Color.Green; 
     } 
     else 
     { 
      txt_course.Text = "invalid Course"; 
      txt_course.ForeColor = Color.Red; 
     } 

     if (phone.Length < 8) 
     { 
      txt_phone.Text = "invalid Phone Number"; 
      txt_phone.ForeColor = Color.Red; 
     } 
     else 
     { 
      txt_phone.ForeColor = Color.Green; 
     } 

    } 
+0

대답은 내가 bool 형식의 검증 방법을 만들고 거기에 내 코드를 넣어 제외하고 내가하는 일입니다. – Missy

답변

1

부울을 추가하고 유효성 검사를 통과하지 못한 곳에서는 false로 설정할 수 있습니다.

private void btn_submit_Click(object sender, EventArgs e) 
{ 
    string name = txt_name.Text; 
    string email = txt_email.Text; 
    string address = txt_address.Text; 
    string course = txt_course.Text; 
    string phone = txt_phone.Text; 
    bool formIsValid = true; 


    if (name.Length < 8) 
    { 
     txt_name.Text = "Invalid Name"; 
     txt_name.ForeColor = Color.Red; 
     formIsValid = false; 
    } 
    else 
    { 
     txt_name.ForeColor = Color.Green; 

    } 

    if (email.Contains('@')) 
    { 
     if (email.Contains(".com") || email.Contains(".COM")) 
     { 

      txt_email.ForeColor = Color.Green; 
     } 
     else 
     { 
      txt_email.Text = "invalid Email"; 
      txt_email.ForeColor = Color.Red; 
      formIsValid = false; 
     } 
    } 
    else 
    { 
     txt_email.Text = "invalid Email"; 
     txt_email.ForeColor = Color.Red; 
     formIsValid = false; 
    } 

    if (address.Length < 12) 
    { 
     txt_address.Text = "invalid Address"; 
     txt_address.ForeColor = Color.Red; 
     formIsValid = false; 
    } 
    else 
    { 
     txt_address.ForeColor = Color.Green; 
    } 
    if (course.Contains("Games Design") || course.Contains("Electronics") || course.Contains("Mobile Communications") || course.Contains("GAMES DESIGN") || course.Contains("ELECTRONICS") || course.Contains("MOBILE COMMUNICATIONS")) 
    { 
     txt_course.ForeColor = Color.Green; 
    } 
    else 
    { 
     txt_course.Text = "invalid Course"; 
     txt_course.ForeColor = Color.Red; 
     formIsValid = false; 
    } 

    if (phone.Length < 8) 
    { 
     txt_phone.Text = "invalid Phone Number"; 
     txt_phone.ForeColor = Color.Red; 
     formIsValid = false; 
    } 
    else 
    { 
     txt_phone.ForeColor = Color.Green; 
    } 

    if (formIsValid) 
    { 
     //submit the form 
    } 
    else 
    { 
     MessageBox.Show("Your error message here"); 
    } 

} 
1

그것은 간단한 일이야, 당신은 거짓 만드는 잘못된 항목의 경우, (IsAllValidEntries를 말) 처음 true로 설정, 상태를 나타 내기 위해 부울 변수를 도입 할 수 있습니다. 조건 변수 중 하나라도 false이면 부울 변수의 값도 false가됩니다. 다음 코드는 당신을 도울 것입니다 :

private void btn_submit_Click(object sender, EventArgs e) 
{ 
    // definitions 

    bool IsAllValidEntries = true; 

    if (name.Length < 8) 
    { 
     //code here 
     IsAllValidEntries = false; 
    } 
    else{ } 

    if (email.Contains('@')) 
    { 
     if (email.Contains(".com") || email.Contains(".COM")) 
     { 
      // your code here 
     } 
     else 
     { 
      //code here 
      IsAllValidEntries = false; 
     } 
    } 
    else 
    { 
     //code here 
     IsAllValidEntries = false; 
    } 

    if (address.Length < 12) 
    { 
     //code here 
     IsAllValidEntries = false; 
    } 
    else 
    { 
     txt_address.ForeColor = Color.Green; 
    } 
    if (course.Contains("Games Design") || course.Contains("Electronics") || 
    { 
     txt_course.ForeColor = Color.Green; 
    } 
    else 
    { 
     //code here 
     IsAllValidEntries = false; 
    } 

    if (phone.Length < 8) 
    { 
     //code here 
     IsAllValidEntries = false; 
    } 
    else 
    { 
     txt_phone.ForeColor = Color.Green; 
    } 

    if (IsAllValidEntries) 
     MessageBox.Show("Well done"); 
    else 
     MessageBox.Show("oooops!");  
} 
1

다른 답변은 잘 작동하지만, 그것의 자신의 방법에 더 좋은 것, 모든 항목이 유효해야하는 경우, 단락 회로의 경우 수, 부울을 반환 AllEntriesValid을 말하고 잘못된 항목이 같은 타격 :

private bool AllEntriesValid() 
{ 
    if (name.Length < 8) 
    { 
     txt_name.Forecolor = Color.Red; 
     return false; 
    } 
    if (email.Contains('@')) 
    { 
     return false; 
    } 

    //if we get this far, no invalid entries were found, return true 
    return true; 
} 

이것은 다음 여러 버튼 클릭에서 호출 할 수 있습니다,뿐만 아니라 하나는 당신이 현재 가지고 - 그리고 훨씬 더 읽기이고 코드가 응축 유지!

는 그런 다음 버튼을 코드에서, 당신은 단순히 부를 것이다 :

private void btn_submit_Click(object sender, EventArgs e) 
{ 
    if (AllEntriesValid()) 
    { 
     //do something now that everything is valid 
    } 
} 

// 편집 : 프레스 너무 빨리 제출합니다. 주어진

관련 문제