如何创建WinForm的登录对话框,并不断循环 [英] How to create winform login dialog and keep looping

查看:193
本文介绍了如何创建WinForm的登录对话框,并不断循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个登录表单,然后将进入主窗体凭据是否正确。这是基本的伪code:

Creating a login form that will then proceed to the main form if the credentials are correct. Here is the basic pseudocode:

ShowLoginForm()

if (DialogResult == OK)
  CheckCredentials();
  if (credentials == VALID)
    ShowMainForm();
  else
    LoopBackAndShowLoginFormAgain();  //repeat process...
else
  CloseLoginForm();

不过,我是比较新的WinForms和仍然认定它了。我试图决定把我的回路(主()或介于LoginForm的)。我应该在哪里进行检查凭据?这是我到目前为止有:

However, I'm relatively new to WinForms and still figuring it out. I'm trying to decide where to put my loop (Main() or somewhere in LoginForm). Where should I make the check for credentials? This is what I have so far:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LoginForm loginForm = new LoginForm();
    if (loginForm.ShowDialog() == DialogResult.OK)
    {
        Application.Run(new AutoSignerForm());
    }
}

我不想退出,并多次打开了一个登录表单(找一些效率,在这里,所以我想用相同的登录对话框)。任何指针,提示或建议?

I don't want to exit and open up a login form repeatedly (am looking for some efficiency here so I want to use the same login dialog). Any pointers, tips, or ideas?

推荐答案

我要做到以下几点:

  • 在Program.cs中显示的登录表单作为对话框

  • in Program.cs show the loginForm as a dialog

LoginForm login_form = new LoginForm();
if(login_form.ShowDialog() == DialogResult.OK) {
    Application.Run(new MainForm());
}

  • 在一个LoginDialog,手柄上的登录按钮,单击事件(或其他被命名)

  • in LoginDialog, handle the click event on "Login" button (or whatever is named)

    // replace with the actual login
    if(textBoxUsername.Text == "my user" && textBoxPassword.Text == "my pass") {
       // save the user has logged in somewhere
       // set the dialog result to ok
       this.DialogResult = DialogResult.OK;
       // close the dialog
       this.Close();
    } else {
       // login failed
       MessageBox.show("Login failed");
       // do not close the window
    }
    

  • 现在,你能不能让显示用的LoginDialog直到用户输入有效的凭据或他放弃了尝试和应用程序关闭。这样,您将有LoginForm的只有一个实例,这将是一个很好的用户体验。 此外,您还可以肯定的是,MainForm中没有在成功启动并显示没有用户登录。

    Now, you will keep the LoginDialog shown until the user enters valid credentials or he gives up trying and the application closes. This way you will have only one instance of the LoginForm and it will be a nice user experience. Also, you can be sure that the MainForm is not initialized and shown without the user logging in successfully.

    这篇关于如何创建WinForm的登录对话框,并不断循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆