如何使功能继续运行

时间:2019-01-02 08:14:14

标签: c# winforms login

我正在构建程序并具有执行按钮。我要确保,如果这是您第一次使用该程序,则需要登录。如果不是,那么您不需要,因为登录凭据已保存。这是我的代码。.现在,它不继续执行其余的execute函数。

loginSaver是一种get and set方法,可以在loginform中将logincontinue设置为true。

IB表格:

internal void ExecBtn_Click(object sender, EventArgs e)
    {
        if (Username != "" || Password != "")
        {
            Username = Properties.Settings.Default.UserName.ToString();
            Password = Properties.Settings.Default.UserPass.ToString();
        }
        else if (loginForm.LoginSaver == true)
        {
            Username = loginForm.LoginUserName;
            Password = loginForm.LoginPassword;
        }
        else if (Username == "" || Password == "")
        {
            loginForm.Show();
            return;
        }



        if (ServerComboBox.SelectedIndex == -1)
        {
                ServerComboBox.BackColor = Color.LightYellow;
                MessageBox.Show("Du måste välja en kund!");

                return;
        }

LoginForm:

private void LoginBtn_Click(object sender, EventArgs e)
        {
            IB_Ärende IBForm = new IB_Ärende();
            if (UserNameTxt.Text == "" || PasswordTxt.Text == "")
            {
                UserNameTxt.BackColor = Color.LightYellow;
                PasswordTxt.BackColor = Color.LightYellow;
                UserNameTxt.ForeColor = Color.Red;
                PasswordTxt.ForeColor = Color.Red;
                MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
            }

            if (CheckBoxSave.Checked == true)
            {
                    IsServerConnected();
                    if (testingConnection == true)
                    {
                        Properties.Settings.Default.UserName = UserNameTxt.Text;
                        Properties.Settings.Default.UserPass = PasswordTxt.Text;
                        Properties.Settings.Default.Save();
                        //loginContinue = true;
                        this.Hide();
                        UserNameTxt.Text = "";
                        PasswordTxt.Text = "";

                        //IBForm.ExecBtn_Click(sender, e);
                    }
                    else if (testingConnection == false)
                    {
                        MessageBox.Show("Fel användarnamn eller lösenord");
                        UserNameTxt.BackColor = Color.LightYellow;
                        PasswordTxt.BackColor = Color.LightYellow;
                        UserNameTxt.ForeColor = Color.Red;
                        PasswordTxt.ForeColor = Color.Red;
                        return;
                    }

            }
            else if (CheckBoxSave.Checked == false)
            {
                IsServerConnected();
                if (testingConnection == true)
                {
                    userName = UserNameTxt.Text;
                    passWord = PasswordTxt.Text;

                    loginchecker = true;
                    loginContinue = true;
                    this.Hide();
                    //IBForm.ExecBtn_Click(sender, e);
                    UserNameTxt.Text = "";
                    PasswordTxt.Text = "";
                }
                else if (testingConnection == false)
                {
                    MessageBox.Show("Fel användarnamn eller lösenord");
                    UserNameTxt.BackColor = Color.LightYellow;
                    PasswordTxt.BackColor = Color.LightYellow;
                    UserNameTxt.ForeColor = Color.Red;
                    PasswordTxt.ForeColor = Color.Red;
                    return;

                }

            }

            else
            {
                MessageBox.Show("Du måste logga in för att kunna fortsätta!");
            }

        }

感谢您的帮助...

1 个答案:

答案 0 :(得分:0)

1。如果要阻止当前函数并等待LoginForm关闭,请使用ShowDialog()而不是Show()。这样,在关闭登录表单后,您可以访问登录名/密码:

loginForm.ShowDialog();
Username = loginForm.LoginUserName;
Password = loginForm.LoginPassword;
if(string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) 
{
    return;
}

2。我建议使用string.IsNullOrEmpty()检查是否提供了登录名/密码。

3。您可以写if (CheckBoxSave.Checked == true)代替if (CheckBoxSave.Checked)

4。如果您的条件得出的结果为True / False,例如if (CheckBoxSave.Checked == true)在其他情况下请勿使用其他if之类的else if (CheckBoxSave.Checked == false)如果不正确,则必须使用

if (CheckBoxSave.Checked) 
{ 
... 
} 
else 
{
...
// checkbox is not checked 
}

if (testingConnection == true)相似

您也可以稍微重构LoginBtn_Click(object sender, EventArgs e)方法:

private void LoginBtn_Click(object sender, EventArgs e)
{
    userName = UserNameTxt.Text;
    passWord = PasswordTxt.Text;

    if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
    {
        UserNameTxt.BackColor = Color.LightYellow;
        PasswordTxt.BackColor = Color.LightYellow;
        UserNameTxt.ForeColor = Color.Red;
        PasswordTxt.ForeColor = Color.Red;
        MessageBox.Show("Du måste ange ett användarnamn och Lösenord!");
        return;
    }
    IsServerConnected();
    if (testingConnection)
    {
        if(CheckBoxSave.Checked)
        {
            Properties.Settings.Default.UserName = UserNameTxt.Text;
            Properties.Settings.Default.UserPass = PasswordTxt.Text;
            Properties.Settings.Default.Save();
        }
        DialogResult = DialogResult.OK;
    } 
    else 
    {
        UserNameTxt.BackColor = Color.LightYellow;
        PasswordTxt.BackColor = Color.LightYellow;
        UserNameTxt.ForeColor = Color.Red;
        PasswordTxt.ForeColor = Color.Red;
        MessageBox.Show("Fel användarnamn eller lösenord");
    }
}

这样,仅当登录成功并且始终设置userName / password时,loginForm.ShowDialog()才是DailogResult.OK。