简体   繁体   English

在登录表单上输入用户名和密码后,处理登录表单并显示主表单

[英]Dispose login form and show main form after username and password are entered on login form

I run LoginForm using the code below: 我使用下面的代码运行LoginForm:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new LoginForm());
    //Application.Run(new MainForm());
}

When user presses the enter button, i want to dispose or close the LoginForm and want to run the MainForm but i got this exception: "InvalidOperationException was unhandled by user code" 当用户按下回车键时,我想处理或关闭LoginForm并想运行MainForm,但我得到了这个异常:“InvalidOperationException未被用户代码处理”

Click handler of the enter button is given below: 点击进入按钮的处理程序如下:

private void LoginFormEnterButton_Click(object sender, EventArgs e)
{
    try
    {
        MainForm a = new MainForm();
        a.Show();
        this.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

What can i do to dispose the LoginForm and after that to run the MainForm successfully? 我可以做什么来处理LoginForm,然后成功运行MainForm?

You can't dispose the Main window. 您无法释放主窗口。 You need to change the way. 你需要改变方式。

First of all set MainForm as startup window. 首先将MainForm设置为启动窗口。

Application.Run(new MainForm());

Load/show the LoginForm in constructor of MainForm, 在MainForm的构造函数中加载/显示LoginForm,

public MainForm()
{
 LoginForm login=new LoginForm();
 login.ShowDialog();
 InitializeComponent();
}

and Click handler code in LoginForm, 并单击LoginForm中的处理程序代码,

private void LoginFormEnterButton_Click(object sender, EventArgs e)
{
   this.Close();
}

I think you should change your approach. 我认为你应该改变你的做法。

Start running your main form. 开始运行主表单。
In the Load event of the main form, run the login form modally 在主窗体的Load事件中,以模态方式运行登录表单
Do the validation inside the login form and return DialogResult.OK. 在登录表单中进行验证并返回DialogResult.OK。
In the Main form continue or exit depending on the return from Login form. 在主窗体中继续或退出,具体取决于登录表单的返回。

[STAThread] 
static void Main() 
{     
    Application.EnableVisualStyles();     
    Application.SetCompatibleTextRenderingDefault(false);     
    Application.Run(new MainForm()); 
} 

public void MainForm_Load(object sender, EventArgs e)
{
     // The using statement will take care of closing and disposing the login form
     using(LoginForm f = new LoginForm())
     {
         if(f.ShowDialog() != DialogResult.OK)
         {
              Application.Exit();
              return;
         }
     }

     // continue with main form processing
}

I think the main application loop is ending when close the login form. 我认为主要的应用程序循环在关闭登录表单时结束。 As an alternative, could you run the app with your mainform, and have it display the login form as a modal dialog. 作为替代方案,您可以使用mainform运行应用程序,并将登录表单显示为模式对话框。 Then if the user doesn't log in successfully you can close the app 然后,如果用户未成功登录,您可以关闭该应用程序

I use an ApplicationContext instead of passing a form. 我使用ApplicationContext而不是传递表单。

   Application.Run(new TerminalApplicationContext());

in the ctor i show a form 在ctor我展示一个表格

public TerminalApplicationContext()
{
    login = new FormLogin();
    login.Logon += new FormLogin.LogonDelegate(login_Logon);
    login.Show();
}

Then in the delegate 'login_logon' i dispose the 'FormLogin' and show another one. 然后在委托'login_logon'中我处理'FormLogin'并显示另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM