C# 单击登录按钮时从一种形式重定向到另一种形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18515737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:28:02  来源:igfitidea点击:

Redirecting from one form to other form on clicking login button

c#winforms

提问by DoIt

I am trying to add a security module to my database application and what actually I am trying to do is whenever I click on Logon button, it exits the login form and opens the main form depending on the role of user. I have the following code:

我正在尝试向我的数据库应用程序添加一个安全模块,实际上我想要做的是每当我单击登录按钮时,它都会退出登录表单并根据用户的角色打开主表单。我有以下代码:

LOGIN FORM:

登录表格:

public void Login()
    {
        frmCommissionReport _commReport = new frmCommissionReport();

        if (_commission.Login(cbxLoginName, txbPassword))
        {
            MessageBox.Show("Successfull");
             // close this form - do not exit the application
            frmCommissionReport frm = new frmCommissionReport();
            this.Close();
            frm.ShowDialog();

        }
        else
        {

            MessageBox.Show("Username or Password not recognised");

        }

    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        Login();

    }

MAIN FORM (frmCommissionReport)

主表格(frmCommissionReport)

public frmCommissionReport()
    {

        InitializeComponent();

        _login.ShowDialog();

    }

But for some reasons whenever I fill in the login details and click Login Button, it again opens the same Login Form and keeps on duplicating the login form whenever I click Login Button rather than redirecting me to Main Form

但是由于某些原因,每当我填写登录详细信息并单击登录按钮时,它都会再次打开相同的登录表单,并在我单击登录按钮时不断复制登录表单,而不是将我重定向到主表单

enter image description here

在此处输入图片说明

采纳答案by DoIt

I figured the apt solution for my problem in a bit different way than I started with

我以与开始时有点不同的方式为我的问题找到了合适的解决方案

LOGIN FORM

登录表格

public partial class frmLogin : Form
{

    #region "Properties"

    private bool _Authenticated = false;

    public bool Authenticated
    {
        get { return _Authenticated; }
        set { _Authenticated = value; }
    }

    #endregion
 public void Login()
    {

        if (GetLoginAuthentication(cbxLoginName, txbPassword))
        {
            this.Hide();
            //MessageBox.Show("Successfull");
            Authenticated = true;
            //frmCommissionReport frm = new frmCommissionReport();
            //frm.ShowDialog();


        }
        else
        {
            Authenticated = false;
            MessageBox.Show("Username or Password not recognised");

        }

    }

MAIN FORM

主要形式

public frmCommissionReport()
    {
        frmLogin login = new frmLogin();
        login.ShowDialog();

        if (login.Authenticated)
        {// block of code
}

This worked fine without any issue

这工作正常,没有任何问题

回答by Thabo

When you login successfully, you are instantiating another frmCommissionReport(), which is in turn calling _login.ShowDialog() (I'm assuming on a new instance of Login), and this.Close() is closing the existing Login form, not the new one associated with the new frmCommissionReport() instance.

当您成功登录时,您正在实例化另一个 frmCommissionReport(),它依次调用 _login.ShowDialog()(我假设在登录的新实例上),而 this.Close() 正在关闭现有的登录表单,而不是与新的 frmCommissionReport() 实例关联的新实例。

回答by Luis

try to change this.close into this.hide

尝试将 this.close 更改为 this.hide

if (_commission.Login(cbxLoginName, txbPassword))
    {
        MessageBox.Show("Successfull");
         // close this form - do not exit the application
        frmCommissionReport frm = new frmCommissionReport();
        this.hide();
        frm.ShowDialog();

    }