当前位置: 代码迷 >> C# >> WINFROM 自动登录效能
  详细解决方案

WINFROM 自动登录效能

热度:96   发布时间:2016-05-05 03:54:40.0
WINFROM 自动登录功能
本帖最后由 yj01263431 于 2015-04-30 09:49:40 编辑
我想实现的是类似QQ的自动登录功能
用户名密码我是存在数据库里的,自己都能获取到。
现在的问题是,我点登录的时候能正常登录,但是把登录按钮执行的方法放到Form_Load事件里
登录就出问题了 ,登录后的界面是出来了,但是登录界面没有隐藏掉,是什么原因?
下面的我写的代码:
        private void btnLogin_Click(object sender, EventArgs e)
        {
            LoginSystem();
        }
  private void LoginSystem()
        {
            try
            {
                //Thread fThread = new Thread(new ThreadStart(SleepT));
                //fThread.Start();
                //btnLogin.Text = "取 消";
                //Application.DoEvents();
                if (txtPassWord.Text.Trim().Equals(string.Empty))
                {
                    lblPassWordMsg.Text = "密码不能为空";
                    return;
                }
                string strJson = GetStrJson();
                if (!IsNormal(strJson))
                {
                    return;
                }
                if (login.IsLoginSuccess(strJson))
                {
                    if (!login.IsJoinSupplier(strJson))
                    {
                        lblUserNameMsg.Text = "该账户未入驻";
                        cmbUserName.Focus();
                        return;
                    }
                    picPwd.Visible = true;
                    if (!File.Exists(dbPath))
                    {
                        System.Data.SQLite.SQLiteConnection.CreateFile(dbPath);
                        login.CreateTable();
                    }
                    login.InsertLoginInfo(cmbUserName.Text.Trim(), txtPassWord.Text.Trim(), DateTime.Now.ToString());
                    string isRemember = chkRemember.Checked ? "1" : "0";
                    string isAutoLogin = chkAutoLogin.Checked ? "1" : "0";
                    login.UpdateSystemSetting(cmbUserName.Text.Trim(), true, "1", isAutoLogin, "30", Business.DefaultPrinter(), "30", "7");
                    login.SetSysAttribute(cmbUserName.Text.Trim(), "记住密码", isRemember);
                    login.SetSysAttribute(cmbUserName.Text.Trim(), "开机时自动为我登录", isAutoLogin);

               
                    Business.userName = cmbUserName.Text.Trim();
                    this.Hide();
                    FrmMain fMain = new FrmMain();
                    fMain.Show();
                }
                else
                {
                    lblPassWordMsg.Text = "密码有误请重新输入";
                }

            }
            catch (Exception ex)
            {
                lblPassWordMsg.Text = "无法登录,请检查网络是否连接上";
            }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
                BindCmbUserName();
                BindChk();
                if (chkAutoLogin.Checked)
                {
                    LoginSystem();
                }
        }

        private void BindCmbUserName()
        {
            try
            {
                cmbUserName.DataSource = login.GetAllLoginUser();
                cmbUserName.DisplayMember = "username";
                cmbUserName.ValueMember = "username";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

   private void BindChk()
        {
            if (login.GetAttrValue("记住密码") == "1")
            {
                chkRemember.Checked = true;
                txtPassWord.Text = login.GetLastPassWord();
            }
            else
            {
                chkRemember.Checked = false;
                txtPassWord.Text = string.Empty;
            }
            if (login.GetAttrValue("开机时自动为我登录") == "1")
            {
                chkAutoLogin.Checked = true;
            }
            else
            {
                chkAutoLogin.Checked = false;
            }
        }
        
------解决思路----------------------
Load事件,执行this.Hiden估计不行,因为线程在存在与被隐藏的登录窗体。
你可以在Main事件处理,当登录窗体DialogResult是OK的时候,Application.Run(主窗体)。
------解决思路----------------------

FrmLogin login = new FrmLogin();
            login.ShowDialog();//登录按钮事件,设置this.DialogResult = DialogResult.OK
            if (login.DialogResult == DialogResult.OK)
            {
                Application.Run(new FrmMain());
            }
  相关解决方案