如果返回是假的,程序不会停止?

时间:2013-09-29 12:22:07

标签: c#

这是我的代码:

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

,我如何让程序停止

1 个答案:

答案 0 :(得分:3)

男孩......

所有方法:

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()应该做得很好

相关问题