winforms 我怎样才能使函数继续运行

s1ag04yj  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(182)

我正在构建一个程序,并且有一个执行按钮。我希望如果这是你第一次使用这个程序,你需要登录。如果不是,那么你不需要,因为登录凭据已经保存。这是我的代码。现在它不继续与执行函数的其余部分。
loginSaver是一个获取和设置方法,它在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;
        }

登录表单:

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!");
            }
            
        }

谢谢你帮忙...

isr3a4wc

isr3a4wc1#

1.如果您想阻止当前函数并等待LoginForm关闭,请使用ShowDialog()而不是Show()。这样,在关闭登录窗体后,您可以访问Login/Password:

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

2.我建议使用string.IsNullOrEmpty()来检查是否提供了登录名/密码。
3.您可以写入if (CheckBoxSave.Checked)来代替if (CheckBoxSave.Checked == true)
4.如果您有评估为True/False的条件,例如else中的if (CheckBoxSave.Checked == true),则不要使用另一个if,如else if (CheckBoxSave.Checked == false)。如果某个条件不为True,则必须为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");
    }
}

这样,loginForm.ShowDialog()将是DailogResult。只有在登录成功时才确定,并且将始终设置用户名/密码。

相关问题