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

最近要做一个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!");
            }
        }
    }
}


发布了20 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/mt122/article/details/7567750