2016-07-21 66 views
1

我做了一個C#窗體來檢查所有條目,當所有條目都有效時,會出現一個消息框,但我需要幫助確保所有條目都有效並顯示一個消息框。可能更容易做到這一點,但不妨學習如何做到這一點。這是迄今爲止我所擁有的。C#窗體檢查所有條目,然後顯示消息框

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

這是一個簡單的事情,你可以引入一個布爾變量來表示狀態,它最初設定爲true(比如IsAllValidEntries),在無效的條目的情況下,使假的。並在End處檢查變量,如果任何條件爲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返回boolean,如果所有參賽作品必須是有效的,可以短路,如果無效的項打,因爲這樣的:

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

//編輯:按下太快提交。

相關問題