2013-09-29 17 views
1

這裏是我的代碼:如果返回錯誤,程序不會停止?

namespace WindowsFormsApplication2 
{ 
public partial class Form2 : Form 
{ 
    private void Form2_Load(object sender, EventArgs e) 
    { 
     pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; 
    } 

    public Form2() 
    { 
     InitializeComponent(); 
    } 
    public bool radioButtons() 
    { 
     if (!userRadioButton.Checked && !adminRadioButton.Checked) 
     { 
      MessageBox.Show("You must select an account type"); 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     bool a = radioButtons(); 
     if (a == true) 
     { 
      string userName = userNameBox.Text; 
      string password = passwordBox.Text; 
      var userNames = File.ReadAllLines(@"C:\Other\myFile.txt"); 
      checkUsernameValid(); 
      checkUsernameNotExist(); 
      checkPasswordsValid(); 
      checkPasswordsMatch(); 
      allOK(); 
     } 
    } 
    public void mySW() 
    { 
     string path = @"C:\Other\myFile.txt"; 
     string userName = userNameBox.Text; 
     string password = passwordBox.Text; 
     using (StreamWriter writer = new StreamWriter(path, true)) 
     { 
      writer.WriteLine("Username and Password: {0} {1}",userName,password); 
      writer.WriteLine(); 
      writer.Close(); 
      writer.Dispose(); 
     } 
     MessageBox.Show("Thanks for registering! \n\nYou may now log in!","Registration SuccessFul"); 
     Application.OpenForms[0].Show(); 
     this.Close(); 
    } 
    public bool checkUsernameNotExist() 
    { 
     if (userNameBox.Text == "") 
     { 
      MessageBox.Show("Username cannot be empty", "Invalid Username Entry"); 
      return false; 
     } 
     else 
      return true; 
    } 
    public bool checkPasswordsMatch() 
    { 
     if (!passwordBox.Text.Equals(repeatPasswordBox.Text)) 
     { 
      MessageBox.Show("Sorry, your passwords do not match, try again", "Password Error"); 
      passwordBox.Text = ""; 
      repeatPasswordBox.Text = ""; 
      return false; 
     } 
     else 
      return true; 
    } 
    public void checkUsernameValid() 
    { 
     if (userNameBox.Text.Contains("Username: " + userNameBox.Text)) 
     { 
      MessageBox.Show("Sorry, that user name is not available, try again", "Invalid Username Entry"); 
      userNameBox.Text = ""; 
      passwordBox.Text = ""; 
      repeatPasswordBox.Text = ""; 
      return false; 
     } 
     else 
      return true; 
    } 
    public void allOK() 
    { 
     if (!userNameBox.Text.Contains("Username: " + userNameBox.Text) && passwordBox.Text == repeatPasswordBox.Text) 
      { 
       mySW(); 
      } 
    } 
    public void checkPasswordsValid() 
    { 
     if (passwordBox.Text == "") 
      { 
       MessageBox.Show("Password fields cannot be empty","Password Error"); 
      } 
    } 
    } 
} 

現在的問題是,如果,例如,用戶名是無效的,而一個消息框顯示出來,那麼它顯示了一個密碼箱,然後登錄框的感謝。如果其中一個結果顯示一個框並返回false,我該如何獲得程序停止?

+0

關閉()表單。 – Ralf

+0

你的意思是哪種形式? – user2827904

+0

如何在'checkUsernameValid()' – usercr

回答

4

Ø孩子......

所有的方法:

checkUsernameValid 
checkUsernameNotExist 
checkPasswordsValid 
checkPasswordsMatch 

應該返回一個布爾值。

public bool checkPasswordsValid() 
{ 
    if (passwordBox.Text == "") 
    { 

     MessageBox.Show("Password fields cannot be empty","Password Error"); 
     return false; 
    } 

    return true; 
} 

public bool checkUsernameValid() 
{ 
    if (userNameBox.Text.Contains("Username: " + userNameBox.Text)) 
    { 
     MessageBox.Show(
      "Sorry, that user name is not available, try again", 
      "Invalid Username Entry"); 

     userNameBox.Text = ""; 
     passwordBox.Text = ""; 
     repeatPasswordBox.Text = ""; 
     return false; 
    } 

    return true; 
} 

,在button1_Click方法,你不應該繼續,如果最後一個沒有通過:

if(checkUsernameValid() && checkUsernameNotExist() && 
    checkPasswordsValid() && checkPasswordsMatch()) 
{ 
    allOK(); 
} 

在這裏,你可以把一個else語句,然後你需要什麼停止該程序。 如果你身體需要結束程序,Close()應該做得很好

+0

它的工作!但它是如何工作的?它並沒有說它們是否都返回true然後執行? – user2827904

+0

@ user2827904這就是它所說的。 'if(f()&& g()&& h()){i(); }'執行'我();'如果'f()&& g()&& h()'評估爲true。由於'&&'如何工作,這意味着如果'f()'返回true,'g()'也返回true,'h()'也返回true。 – hvd

+0

@ user2827904或者你是否意指你期望'if(f()== true && g()== true && h()== true){i(); }'?這是有效的,但意味着完全相同的事情。因爲「false == true」評估爲「false」,而「true == true」評估爲「true」,所以將布爾值與「true」進行比較會簡單地返回布爾值。 – hvd