简体   繁体   English

C#中的两个不同窗口形式

[英]Two different windows forms in C#

I am new to C# and .NET programming. 我是C#和.NET编程的新手。 I want to design an application that opens up with a small login screen and when user presses the "Login" button, my program should close the login form and pass to a new form. 我想设计一个使用小登录屏幕打开的应用程序,当用户按下“登录”按钮时,我的程序应该关闭登录表单并传递给新表单。 How can I achieve this simple process? 我怎样才能实现这个简单的过程?

Thanks. 谢谢。

This could be a solution; 这可能是一个解决方案;

In LoginForm; 在LoginForm中;

public bool IsLoggedIn { get; private set;}
public void LoginButton_Click(object sender, EventArgs e)
{
  IsLoggedIn = DoLogin();
  if(IsLoggedIn)
  {
    this.Close()
  }
  else
  {
    DoSomethingElse();
  }
}

In program.cs 在program.cs中

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  LoginForm loginForm = new LoginForm();
  Application.Run(loginForm);
  if (loginForm.IsLoggedIn)
  {
    Application.Run(new OtherForm());
  }
}

Depending on the overall architecture of your application, I often like to let the main form control launching the login screen. 根据应用程序的整体架构,我经常希望让主窗体控件启动登录屏幕。

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

    //MainForm.cs
    private void MainForm_Load(object sender, EventArgs e)
    {
        this.Hide();
        Login login = new Login();
        if (login.ShowDialog() == DialogResult.OK)
        {
            //make assignments from login
            currentUser = login.User;
        }
        else
        {
            //take needed action
            Application.Exit();
            return;
        }
    }

The challenge here is what form you pass to the Application.Run() method. 这里的挑战是传递给Application.Run()方法的形式。 If you start the application with an instance of the login form and then close that form I believe the application will exit. 如果您使用登录表单的实例启动应用程序,然后关闭该表单,我相信该应用程序将退出。 No doubt there are several ways to handle this... 毫无疑问,有几种方法可以解决这个问题......

One way is to pass an instance of your main form to the Application.Run method (this ties the message loop to that form instance and not the login form). 一种方法是将主表单的实例传递给Application.Run方法(这将消息循环绑定到该表单实例而不是登录表单)。 In the OnLoad method of the main form you can use a modal dialog to perform the login. 在主窗体的OnLoad方法中,您可以使用模式对话框来执行登录。 ie

//--Main form's OnLoad method
protected override void OnLoad(EventArgs ea)
{
    // Remember to call base implementation
    base.OnLoad(ea);

    using( frmLogin frm = new frmLogin() )
    {
        if( frm.ShowDialog() != DialogResult.OK )
        {
            //--login failed - exit the application
            Application.Exit();
        }
    }       
}

If you do use .ShowDialog(), don't forget to wrap it around an using block, or use .Dispose on the login form after you are done. 如果您确实使用.ShowDialog(),请不要忘记将其包装在using块中,或者在完成后使用登录表单上的.Dispose。 Model dialogs must be manually disposed. 必须手动处理模型对话框。

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

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