C# Winform登录成功打开新窗体

本文介绍了在C# WinForm应用中如何实现在登录成功后打开新的窗体并关闭登录窗体。初始尝试的直接关闭方式会导致两个窗体同时关闭,原因是操作发生在主线程上。通过参考网上的资料,解决了这个问题。文章提供了包含UserInfo.cs、LoginForm.CS和Program.cs代码的解决方案。

最近要做一个WinForm程序,需要想QQ那样登录成功后打开一个新的窗体,同时关闭登录窗体。刚开始我是直接Form one=new Form();one.Show();this.Close();这样两个窗体都关闭了,因为是在主线程上面操作。(注意:如果是在其他线程上面使用该方法是可以实现的,但是在主线程无法实现).后来在网上差了资料,根据http://blog.csdn.net/knight94/article/details/652394这里的文章终于实现了我需要的功能。

项目截图:

 

UserInfo.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VideoWatch.AppCode
{
    /// <summary>
    /// 用户登录信息
    /// </summary>
    public class UserInfo
    {
        private string strUserName;
        private string strPassword;
        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName
        {
            get { return strUserName; }
            set { strUserName = value; }
        }
        /// <summary>
        /// 用户密码
        /// </summary>
        public string Password
        {
            get { return strPassword; }
            set { strPassword = value; }
        }
        public UserInfo()
        {
            strUserName = "";
            strPassword = "";
        }
    }
}


LoginFrom.CS代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VideoWatch.AppCode;

namespace VideoWatch
{
    public partial class LoginForm : Form
    {
        LoginForm loginForm = null;

        private int nLoginCount = 0;//登录次数
        private const int MAX_LOGIN_COUNT = 3;//限制只能登录三次

        private UserInfo uiLogin;//用户登录信息

        public LoginForm(ref UserInfo ui)
        {
            InitializeComponent();
            uiLogin = ui;
        }

        /// <summary>
        /// 登录程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "Admin" && txtPassword.Text == "123456")
            {
                // Save login user info
                uiLogin.UserName = txtUserName.Text.Trim();
                uiLogin.Password = txtPassword.Text.Trim();

                // Set dialog result with OK
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                // Wrong username or password
                nLoginCount++;
                if (nLoginCount == MAX_LOGIN_COUNT)
                    // Over 3 times
                    this.DialogResult = DialogResult.Cancel;
                else
                {
                    MessageBox.Show("Invalid user name and password!");
                    txtUserName.Focus();
                }
            }
        }

        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Check whether form is closed with dialog result
            if (this.DialogResult != DialogResult.Cancel &&
                this.DialogResult != DialogResult.OK)
                e.Cancel = true;
        }

    }
}


Program.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using VideoWatch.AppCode;

namespace VideoWatch
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            UserInfo ui = new UserInfo();//用户登录信息
            LoginForm myLogin = new LoginForm(ref ui);//加载登录窗体
            if (myLogin.ShowDialog() == DialogResult.OK)
            {
                //Open your main form here
                //MessageBox.Show("Logged in successfully!");
                Application.Run(new MainForm());//如果登录成功则打开主窗体
            }
            else
            {
                MessageBox.Show("Failed to logged in!");
            }
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值