简体   繁体   English

关闭表格的C#问题

[英]C# issues with closing the form

I have been experiencing problems with closing the Log In form when I have switched to 2nd form on successful Log In. 在成功登录后切换到第二表单时,我在关闭登录表单时遇到问题。 I tried .close and .dispose , but the Log in Form wouldn't close. 我尝试了.close.dispose ,但登录表单无法关闭。 Following is my code. 以下是我的代码。

namespace Lead_Management_Pro
{
    public partial class FrmLogin : Form
    {
        Form1 mainform;
        public FrmLogin()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            FrmLogin frm = new FrmLogin();
            mainform = new Form1();
            string[] v;

            OleDbConnection conn = new OleDbConnection("File Name=E:\\Vivek\\License Manager\\License Manager\\login.udl");

            try
            {
                conn.Open();

                string query = "EXEC dbo.checkuser '" + username.Text+ "', '" + password.Text+"'";
                OleDbCommand cmd = new OleDbCommand(query, conn);
                string s = Convert.ToString(cmd.ExecuteScalar());
                v= s.Split('|');
                if (v[0]=="0")
                {

                    frm.Close();
                    mainform.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Please enter correct user credentials and try again");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            conn.Close();
        }

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

Please help me to rectify the issue. 请帮助我纠正此问题。

frm.Close() closes the instance you are making. frm.Close()关闭您正在创建的实例。 you should use this.Close(); 您应该使用this.Close(); like the one you did in button2_Click event 就像您在button2_Click事件中所做的一样

Update 2 更新2

FrmLogin is your startup form. FrmLogin是您的启动表单。 Try hiding it: 尝试隐藏它:

if (v[0]=="0")
{
    mainform.Show();
    this.Hide();
}

I had a similar issue in one of my projects. 在我的一个项目中,我遇到了类似的问题。 The problem is that if you open your login form as your first form, it's viewed as the main form, and when you close it, the program starts shutting down because you closed your main form. 问题在于,如果您以第一个表单的形式打开登录表单,它将被视为主要表单,并且当您关闭登录表单时,由于关闭了主表单,程序将开始关闭。

What you need to do is open your login form from the context of your Form1 main form, instead of opening your main form from the context of your login form. 您需要做的是从Form1主表单的上下文中打开登录表单,而不是从登录表单的上下文中打开主表单。

in pseudocode: 用伪代码:

public Form1(){
    if(not logged in)
    {
        FrmLogin login = new FrmLogin();
        login.ShowDialog();//This blocks the further execution of the Form1 constructor until you're out of the window
    }
    InitializeCompent();
}

Handle your login procedure in your Login form and close the form in there once you're logged in. It will automatically continue on to the rest of your Form1 constructor and open it through there. 在登录表单中处理登录过程,并在登录后在其中关闭表单。它将自动继续到Form1构造函数的其余部分,并从那里打开它。 You might want to have a check after executing your login whether you're actually logged in, and shut down if you're not. 您可能希望在执行登录后检查是否已实际登录,如果未登录则将其关闭。

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

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