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

发布于 2024-07-11 00:25:08 字数 116 浏览 4 评论 0原文

我是 C# 和 .NET 编程新手。 我想设计一个打开一个小登录屏幕的应用程序,当用户按下“登录”按钮时,我的程序应该关闭登录表单并传递到一个新表单。 我怎样才能实现这个简单的过程?

谢谢。

I am new to C# and .NET programming. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2024-07-18 00:25:08

这可能是一个解决方案;

在登录表单中;

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

在程序.cs中

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

This could be a solution;

In 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

static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  LoginForm loginForm = new LoginForm();
  Application.Run(loginForm);
  if (loginForm.IsLoggedIn)
  {
    Application.Run(new OtherForm());
  }
}
星光不落少年眉 2024-07-18 00:25:08

根据应用程序的整体架构,我经常喜欢让主窗体控制启动登录屏幕。

    //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;
        }
    }

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;
        }
    }
放我走吧 2024-07-18 00:25:08

这里的挑战是您传递给 Application.Run() 方法的形式。 如果您使用登录表单的实例启动应用程序,然后关闭该表单,我相信应用程序将退出。 毫无疑问,有多种方法可以处理这个问题...

一种方法是将主窗体的实例传递给 Application.Run 方法(这将消息循环与该窗体实例而不是登录窗体联系起来)。 在主窗体的 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();
        }
    }       
}

The challenge here is what form you pass to the Application.Run() method. 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). In the OnLoad method of the main form you can use a modal dialog to perform the login. i.e.

//--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();
        }
    }       
}
一桥轻雨一伞开 2024-07-18 00:25:08

如果您确实使用 .ShowDialog(),请不要忘记将其包装在 using 块中,或者在完成后在登录表单上使用 .Dispose。 模型对话框必须手动处理。

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. Model dialogs must be manually disposed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文