C# 界面开发-登陆窗体(包括欢迎界面)

在开发一个需要身份验证系统软件的时候,一个必不可少的界面便是登陆界面,只有我们在登陆界面中输入正确的用户名和密码,才能登陆到主界面,有时候可能还会加载系统资源,所以还会增加一个界面“欢迎界面”来作为加载系统资源等待界面,所以登陆一个系统,会先后出现这三个基本界面:欢迎界面、登陆界面、主界面。

下面我就开始来一步步设计这三个窗体

分别命名为WelcomeForm、LoginForm、MainForm,在起名的时候,我们最好能严格按照控件命名编写规范来命名。

应用程序窗体设计好后,就开始动手写代码

(第一)在programma.cs中的Main函数中,添加代码:

将原来的这部分代码

  static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

改成

  static class Program

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

            //界面转换
            LoginForm loginForm = new LoginForm();
            loginForm.ShowDialog();

            if (loginForm.DialogResult == DialogResult.OK)
            {
                loginForm.Dispose();
                Application.Run(new MainForm());
            }
            else if (loginForm.DialogResult == DialogResult.Cancel)
            {
                loginForm.Dispose();
                return;
            }

        }
    }

第2步:登陆窗体界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AliWorkbenchProgram
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();

            //在此添加代码,在登陆窗体显示前先显示欢迎窗体
            WelcomeForm welcomeForm = new WelcomeForm();
            welcomeForm.Show();//show出欢迎窗口
            System.Threading.Thread.Sleep(5000);//欢迎窗口停留时间2s
            welcomeForm.Close();

            this.StartPosition = FormStartPosition.CenterScreen;
        }

        public string strDataBaseName = "";
        public string strDataTableName = "";
        public string strUName = "";
        public string strUPassWord = "";


        private void BtnLogin_Click(object sender, EventArgs e)
        {

            strDataBaseName = "DBSystem";
            strDataTableName = "TB_Users";
            //获取文本框中的值
            strUName = this.TxtUserName.Text.Trim();
            strUPassWord = this.TxtPassWord.Text.Trim();

            if (strUName.Equals("") || strUPassWord.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
            }
            else//用户名或密码不为空
            {
                //到数据库中验证
                string strsql = "select * from "+ strDataTableName + " where UName='" + strUName + "' and UPassWord='" + strUPassWord + "'";
               // string strsql = "select * from " + strDataBaseName + ".[dbo]." + strDataTableName + " where UName='" + strUName + "'";

                //strsql= "select * from TB_Users where UName = 'wxd'" ;

                ClassSqlClientOp classSqlClientOp = new ClassSqlClientOp();
                DataTable dt = new DataTable() ;
                dt= classSqlClientOp.GetTable(strDataBaseName, strDataTableName, strsql);
                int count = dt.Rows.Count;
                if (count > 0)//如果信息>0则说明匹配成功
                {
                    MessageBox.Show("信息验证成功");

                    //将当前登录日志信息写入数据库(待开发...)

                    // 跳转主界面
                    this.DialogResult = DialogResult.OK;
                    this.Dispose();
                    this.Close();


                    /*
                    AdminForm adminForm = new AdminForm();//创建一个新页面
                    adminForm.Show();//显示新页面
                    this.Hide();//隐藏当前页面
                    */

                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
        }

}

}

}

猜你喜欢

转载自blog.csdn.net/zgscwxd/article/details/86666872