在C#中关闭多个表单 [英] Closing Multiple Forms in C#

查看:137
本文介绍了在C#中关闭多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体应用程序中共有3个窗体(Form1,Form2和Form3)。

Form2是登录页面。当用户点击Form1中的登录按钮时,Form2必须打开,如果用户提供了准确的用户名和密码,则需要Form3来打开和关闭Form1和Form2。



<如何在C#中编写这样的东西?我使用的是Microsoft Visual Studio 2012.



到目前为止,我已经完成了以下程序:

  Form2 secondForm = new Form2(); 

只是在表单加载事件之外



&安培;在按钮内部,我写道 - $ / b>

  secondForm.Show(); 

因此,当我运行解决方案时,通过单击Form1中的按钮打开Form2(Works Perfectly!) 。但我不知道如何在用户在Form2中输入正确的用户名和密码组合以打开Form3时如何关闭Form1和Form2。

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

未关闭表格。

请指导我。

解决方案

如果您不能关闭主窗体(您用来启动消息循环的窗体)这样做会结束/关闭整个应用程序。然而,你可以做的事情是这样的:



Form1代码

 公共部分类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代码:

  public partial class Form2:Form 

{
public Form2()
{
InitializeComponent();
}

private void buttonLogin_Click(object sender,EventArgs e)
{
//如果用户名和密码正确,将对话结果设置为ok
this.DialogResult = DialogResult.OK;

this.Close();
}
}


I have total 3 forms (Form1, Form2 and Form3) in my Windows Forms Application.

Form2 is log-in page. 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.

How to code such thing in C#? I am using Microsoft Visual Studio 2012.

So far I have done following procedure:

double click the Form1 to get at the coding window and wrote-

Form2 secondForm = new Form2();

Just outside of the Form Load event

& inside the button, i wrote-

secondForm.Show();

So when I run the solution, Form2 is opened by clicking a button in Form1 (Works Perfectly!). But I have no idea how to close Form1 and Form2 when user enter correct username and password combination in Form2 to open Form3.

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

isn't closing the form.

Please guide me.

解决方案

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

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:

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();
        }
    }

这篇关于在C#中关闭多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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