LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#制作登陆和注册界面并带有美化窗口

admin
2016年12月27日 22:24 本文热度 6706

1,首先设计登录界面,共有三个,如下:

 

上图登录及注册为linklabel控件,其他为label控件;

 

上图为登陆界面,两个textbox文本输入框,注册为linklabel控件;

界面设计很简单,不说了。

 

2,代码介绍:

1)  主界面(Form1):

 

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

            this.Hide();

            Form3 f3 = new Form3();

            f3.ShowDialog();

        }//显示注册界面;

 private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

            this.Hide();

            Form2 f = new Form2();

            f.ShowDialog();

            if (f.DialogResult == DialogResult.OK)

            {

                this.Visible = true;

            }

        }//显示登录界面;

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {          

           try

                {

       System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();

         foreach (System.Diagnostics.Process myProcess in myProcesses)

                    {

                        if ("LoginInterface.exe" == myProcess.ProcessName)

                            myProcess.Kill();

                    }

                }

          catch (Exception ee)

                {

                   

                    MessageBox.Show(ee.Message);

                }

           }//关掉程序;

2)  注册界面(Form3

本文使用的数据库是sql sever2005,先在引用里加入:

using System.Data.SqlClient;

 

以下为程序代码:

    public partial class Form3 : Form

    {

        public Form3()

        {

            InitializeComponent();

        }

        bool  flagRegister;//定义标志位,确认用户注册      

        string strConnect = "Data Source=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=******";  //连接数据库字符串             

        private void button1_Click(object sender, EventArgs e)

        {

            if ((textBox1.Text.Length >= 4) && (textBox1.Text.Length <= 12) && (textBox2.Text.Length >= 6) && (textBox3.Text.Length >= 6))

            {

                flagRegister = true;

            }

            else

            {

                if ((textBox1.Text.Length < 4) || (textBox1.Text.Length > 12))

                {

                    MessageBox.Show("用户名长度不在约定范围内,请重新输入!", "提示");

                    return;

                }

                if (textBox2.Text.Length < 6)

                {

                    MessageBox.Show("密码长度不足6位,请重新输入!","提示");

                    return;

                }

                if (textBox3.Text.Length < 6)

                {

                    MessageBox.Show("请重新输入邮箱!", "提示");

                    return;

                }

            }//判断用户名条件;

 

            if (UserFlag == true)

            {

                MessageBox.Show("用户已经存在,请重新输入!");

                return;

            }

 

            if (flagRegister == true) //确认用户注册后,把用户写入数据库

            {

                SqlConnection conConnection = new SqlConnection(strConnect);

                conConnection.Open();

                string cmd = "insert into 用户(用户名,密码,email) values (''" + textBox1.Text + "''," +"''" + textBox2.Text + "''," + "''" + textBox3.Text + "'') ";

                SqlCommand com = new SqlCommand(cmd, conConnection);

                com.ExecuteNonQuery();

                conConnection.Close();

                MessageBox.Show("注册成功!点击确定,返回登录界面。", "提示");

                this.Close();

                Form1 f1 = new Form1();

                f1.label2.Text = "欢迎你," + textBox1.Text;

                f1.label1.Visible = false;

                f1.label3.Visible = false;

                f1.linkLabel1.Visible = false;

                f1.linkLabel2.Visible = false;

                f1.label2.Visible = true;

                f1.Show();

            }

          

 

        }

 

        public bool UserFlag; //定义标志位,来确认用户是否存在

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

            SqlConnection conConnection = new SqlConnection(strConnect);

            conConnection.Open();

            string cmd = "select 用户名 from 用户";

            SqlCommand com = new SqlCommand(cmd, conConnection);

            SqlDataReader readerUser = com.ExecuteReader();

            while (readerUser.Read())

            {

                if (textBox1.Text == readerUser["用户名"].ToString().Trim())

                {

                    label5.Text = "用户已存在,请重新输入!";

                    UserFlag = true;

                    //textBox1.Text = "";

                    return;

                }

                else if (textBox1.Text != readerUser["用户名"].ToString().Trim())

                {

                    label5.Text = "恭喜你,该用户名可以使用。";

                    UserFlag = false;

                }

            }

        }//判断用户名是否满足条件

 

        private void textBox3_TextChanged(object sender, EventArgs e)

        {

            int index = textBox3.Text.IndexOf("@");

            if (index < 1)

            {

                label7.Text = "邮箱格式不正确,请重新输入!";

            }

            else

            {

                label7.Text = "邮箱格式正确";

            }

        }//判断邮箱格式是否正确

    }

 

3) 登录界面(Form2

本文使用的数据库是sql sever2005,先在引用里加入:

using System.Data.SqlClient;

 

以下为程序代码:

string User, Pwd; //用户名,密码

bool flagshow = false;//用来标注登录名是否存在于数据库

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

        {

            this.Hide();

            Form3 f3 = new Form3();

            f3.ShowDialog();

        }//显示注册界面

      

 private void button1_Click(object sender, EventArgs e) //登录

        {           

            string strConnect = "Data Source=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=******";

            SqlConnection conConnection = new SqlConnection(strConnect);

            conConnection.Open();

            string cmd = "select 用户名,密码,email from 用户";

            SqlCommand com = new SqlCommand(cmd, conConnection);

            SqlDataReader reader = com.ExecuteReader();

            while (reader.Read())//从数据库读取用户信息

            {

                User = reader["用户名"].ToString();

                Pwd = reader["密码"].ToString();

                if (User.Trim () == textBox1.Text & Pwd.Trim () == textBox2.Text)

                {

                    flagshow = true; //用户名存在于数据库,则为true

                }

            }

            reader.Close();

            conConnection.Close();

 

            if (flagshow == true)

            {

                showMainForm();//用户存在,返回登录界面

            }

            else

            {

                MessageBox.Show("用户不存在或密码错误!", "提示");

                return;

            }

 

        }

 

        private void showMainForm()//登录成功,显示主界面

        {

            this.Close();

            Form1 f1 = new Form1();

            f1.label1.Visible = false;

            f1.label3.Visible = false;

            f1.linkLabel1.Visible = false;

            f1.linkLabel2.Visible = false;

            f1.label2.Visible = true;

            f1.label2.Text = "欢迎你," + textBox1 .Text ;

            f1.Show();

        }

 

3为美化窗体,可下载winform皮肤包,下载地址为:

http://down.51cto.com/data/139603

 

把皮肤文件和IrisSkin2.dll放在bin文件夹下debug文件夹中,把IrisSkin2.dll直接拖进工具箱,即可使用。在Form1加入皮肤控件skinEngine1Form1的构造函数里加入

skinEngine1.SkinFile =皮肤文件路径;

本文为:

skinEngine1.SkinFile = "皮肤\\wave\\WaveColor1.ssk";

就可以得到比较炫的窗体了。本文窗体效果如下:

主界面:

注册界面:

 

注册成功后返回登录界面:

 

登录界面:

 

登录成功:


该文章在 2016/12/27 22:24:52 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved