简体   繁体   English

在C#中关闭多种形式

[英]Closing Multiple Forms in C#

I have total 3 forms (Form1, Form2 and Form3) in my Windows Forms Application. 我的Windows窗体应用程序中共有3个窗体(Form1,Form2和Form3)。

Form2 is log-in page. Form2是登录页面。 When user clicks on sign-in button in Form1, Form2 must be open and if user provides accurate username and password then Form3 is needed to open and to close both Form1 and Form2. 当用户单击Form1中的登录按钮时,必须打开Form2,并且如果用户提供了正确的用户名和密码,则需要Form3来打开和关闭Form1和Form2。

How to code such thing in C#? 如何在C#中编写此类代码? I am using Microsoft Visual Studio 2012. 我正在使用Microsoft Visual Studio 2012。

So far I have done following procedure: 到目前为止,我已经完成了以下过程:

double click the Form1 to get at the coding window and wrote- 双击Form1进入编码窗口并写下-

Form2 secondForm = new Form2();

Just outside of the Form Load event 就在Form Load事件之外

& inside the button, i wrote- 在按钮内,我写道-

secondForm.Show();

So when I run the solution, Form2 is opened by clicking a button in Form1 (Works Perfectly!). 因此,当我运行解决方案时,可以通过单击Form1中的按钮来打开Form2(完美工作!)。 But I have no idea how to close Form1 and Form2 when user enter correct username and password combination in Form2 to open Form3. 但是我不知道当用户在Form2中输入正确的用户名和密码组合以打开Form3时如何关闭Form1和Form2。

Form1 firstForm = new Form1();
firstForm.Close();

isn't closing the form. 没有关闭表格。

Please guide me. 请指导我。

You should add a new Class with a Main() method. 您应该使用Main()方法添加一个新的类。 (OR) There will be Program.cs in your project. (或)您的项目中将有Program.cs。

And, start the application from the Main() method. 并且,从Main()方法启动应用程序。 You can change "Startup object:" property of your project to "<YourApplicationName>.Program". 您可以将项目的“启动对象:”属性更改为“ <YourApplicationName> .Program”。

You Main() method should show Form1 and Form2 as Dialog. 您的Main()方法应将Form1Form2显示为Dialog。

If DialogResult is OK then run the Form3 by Application.Run() method. 如果DialogResult OK则通过Application.Run()方法运行Form3

DialogResult r;
r = (new Form1().ShowDialog());
if( r == DialogResult.OK )
Application.Run(new Form3());

You cannot close the main form (the one that you used to start the message loop) if you do that will end/close the entire application. 如果关闭主窗体(用于启动消息循环的窗体),则该主窗体将结束/关闭整个应用程序。 What you can do however instead is this: 但是,您可以做的是:

Form1 code Form1代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var f2 = new Form2();

        var res = f2.ShowDialog();

        if (res == DialogResult.OK)
        {
            var f3 = new Form3();
            this.Visible = false;
            f3.ShowDialog();
            this.Close();
        }
    }
}

Form2 code: Form2代码:

public partial class Form2 : Form

    {
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            // if username and password is ok set the dialog result to ok
            this.DialogResult = DialogResult.OK;

            this.Close();
        }
    }

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

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