C# winform 主界面打开并关闭登录界面

在winform 界面编程中,我们有时候要在主界面打开之前先显示登录界面,当登录界面用户信息校验正确后才打开主界面,而这时登陆界面也完成使命该功成身退了。

目前有两种方法可实现:

方法1. 隐藏登录界面

    Program.cs 中代码如下:

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }

 

    Login.cs 中代码如下:

        /// <summary>
        /// login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "12345678")
            {
                UI ui = new UI();
                this.Visible = false;
                ui.ShowDialog();//此处不可用Show()
                this.Dispose();
                this.Close();
            }
            else
            {
                MessageBox.Show("Password is incorrect ", "Prompt message",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

 

方法2. 登录界面以 dialog形式打开,返回登录结果 

    Program.cs 中代码如下:

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Login login = new Login();
            login.ShowDialog();
            if (login.DialogResult == DialogResult.OK)
            {
                login.Dispose();
                Application.Run(new UI());
            }
            else if (login.DialogResult == DialogResult.Cancel)
            {
                login.Dispose();
                return; 
            }
        }

    Login.cs 中代码如下:

        /// <summary>
        /// login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "12345678")
            {
                this.DialogResult = DialogResult.OK;
                this.Dispose();
                this.Close();
            }
            else
            {
                MessageBox.Show("Password is incorrect ", "Prompt message",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
            this.Close();
        }

 

posted @ 2017-03-16 18:09  Waming  阅读(14745)  评论(0编辑  收藏  举报