C#之仿魔兽登录

不多废话,直接上效果图:

1录窗体

对应的代码:

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 Game
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
       //点击x按钮触发的事件
        private void btnclose_Click(object sender, EventArgs e)
        {
            //退出整个应用
            Application.Exit();
        }

        //点击注册按钮触发的事件
        private void lblregister_Click(object sender, EventArgs e)
        {
            //隐藏当前窗体
            this.Hide();
            FrmRegist frm = new FrmRegist();
            frm.fl = this;
            frm.Show();
        }


        //点击登录按钮触发的事件
        private void btnlogin_Click(object sender, EventArgs e)
        {
            if (ProvingInfo()==true)
          {
             //定义变量接收文本框中的值
              string email = txtemail.Text;
              string password = txtpassword.Text;
              bool happy = false;
              foreach (LoginInfo item in LoginInfo.array)
              {
                  if(item!=null)
                  {
                      if (item.Email.Equals(email) && item.Password.Equals(password))
                      {
                          happy = true;
                          //关闭当前窗体
                          this.Hide();
                          //显示主窗体
                          FrmMain frm = new FrmMain();
                          frm.loginame = item.Name;
                          frm.Show();
                          break;
                      }
                  
                  }
              }
                if(happy==false)
                {
                    MessageBox.Show("登录失败!请检查邮箱和密码是否正确"); 
                }
          }

        }
        //验证填写信息
        public bool ProvingInfo()
        { 
           if(txtemail.Text.Trim().Equals(string.Empty))
           {
           MessageBox.Show("请填写email");
               this.txtemail.Focus();
               return false;
           }
           else if (txtpassword.Text.Trim().Equals(string.Empty))
           {
               MessageBox.Show("请填写密码");
               this.txtpassword.Focus();
               return false;
           }
           else
           {

               return true;
           }
        
        }
        
        //Load事件
        private void FrmLogin_Load(object sender, EventArgs e)
        {

      
             LoginInfo info1 = new LoginInfo();
            info1.Name = "泪洒星辰";
            info1.Id = "123123131313131";
            info1.Email = "lsxc@163.com";
            info1.Password = "pwd@123";
            for (int i = 0; i < LoginInfo.array.Length; i++)
            {
                if (LoginInfo.array[i] == null)
                {
                    LoginInfo.array[i] = info1;
                    break;
                }
            }
              
            
        }

      
    }
}

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 Game
{
    public partial class FrmRegist : Form
    {
        public FrmRegist()
        {
            InitializeComponent();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
        //点击取消触发的事件
        private void btnclose_Click(object sender, EventArgs e)
        {
            //关闭当前窗体
            this.Close();
            //显示上一级窗体
            FrmLogin fl = new FrmLogin();
            fl.Show();

        }

        private void FrmRegist_Load(object sender, EventArgs e)
        {

        }
        //定义窗体对象保存logininfo窗体传过来的值
        public FrmLogin fl;
        //点击注册触发的事件
        private void btnregin_Click(object sender, EventArgs e)
        {
            
            if (ProvingInfo() == true)
            {
                //获取文本框中对应的值
                string name = txtname.Text;
                string email = txtemail.Text;
                string cid = txtcid.Text;
                string password = txtpassword.Text;
                LoginInfo info = new LoginInfo();
               info.Name = name;
                info.Id = cid;
                info.Email = email;
               info.Password = password;
                MessageBox.Show("注册成功!");
                for (int i = 0; i < LoginInfo.array.Length; i++)
                {
                    if (LoginInfo.array[i] == null)
                    {
                        LoginInfo.array[i] = info;
                        break;
                    }
                }
            }
        }
        //验证填写信息
        public bool ProvingInfo()
        { 
             if(txtname.Text.Trim().Equals(string.Empty))
             {
                 MessageBox.Show("请填写姓名");
                 this.txtname.Focus();
                 return false;
             }
             else if(txtcid.Text.Trim().Equals(string .Empty))
             {
                 MessageBox.Show("请填身份证号码");
                 this.txtcid.Focus();
                 return false;
             
             }
             else if (txtemail.Text.Trim().Equals(string.Empty))
             {
                 MessageBox.Show("请填身份证号码");
                 this.txtemail.Focus();
                 return false;

             }
             else if (txtpassword.Text.Trim().Equals(string.Empty))
             {
                 MessageBox.Show("请填写密码");
                 this.txtpassword.Focus();
                 return false;

             }
             else if (txtone.Text.Trim().Equals(string.Empty))
             {
                 MessageBox.Show("请确认邮箱输入");
                 this.txtone.Focus();
                 return false;

             }
             else if (txtokone.Text.Trim().Equals(string.Empty))
             {
                 MessageBox.Show("请确认密码输入");
                 this.txtokone.Focus();
                 return false;

             }
             else if (!txtemail.Text.Equals(txtone.Text))
             {
                 MessageBox.Show("两次输入的邮箱不一致");
                 return false;
             }
             else if(!txtpassword.Text.Equals(txtokone.Text))
             {
              MessageBox.Show("两次输入的密码不一致");
              return false;
             }
             else 
            {
                return true;
             }
            
        
        }
    }
}

3.主界面

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 Game
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        //定义变量保存窗体传过来的值
        public string loginame;
        //Load事件
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //给label控件赋值
            lblloginname.Text = "欢迎" + loginame + "来到魔兽世界";
        }
    }
}

其实,并不复杂,关键是你的思路,一定要清晰明了。

原文地址:https://www.cnblogs.com/quliang/p/6524692.html