Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果返回为false,程序不会停止?_C# - Fatal编程技术网

C# 如果返回为false,程序不会停止?

C# 如果返回为false,程序不会停止?,c#,C#,这是我的密码: namespace WindowsFormsApplication2 { public partial class Form2 : Form { private void Form2_Load(object sender, EventArgs e) { pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; } public Form2() { InitializeC

这是我的密码:

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,如何使程序停止?

o boy

所有方法:

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;
}
之后,在
按钮1\u单击
方法中,如果最后一个未通过,则不应继续:

if(checkUsernameValid() && checkUsernameNotExist() && 
    checkPasswordsValid() && checkPasswordsMatch())
{
   allOK();
}
在这里,您可以放置一个
else
语句,然后执行停止程序所需的操作。 如果您实际需要结束程序,
Close()
应该可以

o boy

所有方法:

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;
}
之后,在
按钮1\u单击
方法中,如果最后一个未通过,则不应继续:

if(checkUsernameValid() && checkUsernameNotExist() && 
    checkPasswordsValid() && checkPasswordsMatch())
{
   allOK();
}
在这里,您可以放置一个
else
语句,然后执行停止程序所需的操作。
如果您实际需要结束程序,
Close()
应该很好

您指的是哪种形式?在
checkUsernameValid()中返回值时如何编译上述代码
@user2827904用作启动程序的主窗体的窗体。您指的是哪种窗体?在
checkUsernameValid()
@user2827904用作启动程序的主窗体的窗体中返回值时,如何编译上述代码。它起作用了!但是它是如何工作的呢?它并没有说它们是否都返回true,然后执行?@user2827904它就是这么说的<代码>如果(f()&&g()&&h()){i();}
执行
i()如果
f()&&g()&&h()
计算结果为true。由于
&
的工作方式,这意味着如果
f()
返回true,并且
g()
也返回true,
h()
也返回true。@user2827904或者您的意思是,如果(f()==true&&g()==true&&h()==true){i();}
,您希望
?这是正确的,但意思完全一样。将布尔值与
true
进行比较只会返回该布尔值,因为
false==true
的计算结果为
false
,而
true==true
的计算结果为
true
。啊,太棒了。每天学习新的东西。我还有一个问题。。。我可以注册名称并将其转到指定的文件,但我可以使用相同的用户名再次注册???@user2827904如果您想阻止它,您需要保留现有用户名列表并检查新名称是否包含在itit中!但是它是如何工作的呢?它并没有说它们是否都返回true,然后执行?@user2827904它就是这么说的<代码>如果(f()&&g()&&h()){i();}
执行
i()如果
f()&&g()&&h()
计算结果为true。由于
&
的工作方式,这意味着如果
f()
返回true,并且
g()
也返回true,
h()
也返回true。@user2827904或者您的意思是,如果(f()==true&&g()==true&&h()==true){i();}
,您希望
?这是正确的,但意思完全一样。将布尔值与
true
进行比较只会返回该布尔值,因为
false==true
的计算结果为
false
,而
true==true
的计算结果为
true
。啊,太棒了。每天学习新的东西。我还有一个问题。。。我可以注册名称并将其转到指定的文件,但我可以使用相同的用户名再次注册???@user2827904如果要阻止它,您需要保留现有用户名的列表并检查其中是否包含新名称