深入C#第一章项目

本文介绍了一个简单的登录注册系统的实现过程,包括用户信息的存储、登录验证及注册功能。使用C#语言和Windows Forms进行开发,演示了如何创建用户对象、验证用户输入并存储到对象数组中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

namespace FrmLogin
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        //存储登录用户信息的对象数组
        public LoginInfo[] array = new LoginInfo[10];
        private void FrmLogin_Load(object sender, EventArgs e)
        {
           
        }
        public void show() 
        {
            LoginInfo l = new LoginInfo();
            l.Email = "lili@show.com";
            l.ID = "111";
            l.Name = "孙丽丽";
            l.Password = "123";
            array[0] = l;
            //两种方式
            //array[1] = new LoginInfo();
            //array[1].Name = "王";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Trim()==""||this.textBox1.Text.Trim()=="")
            {
                MessageBox.Show("用户或密码不能为空");
            }
            else
            {
                string name = textBox1.Text;
                string pwd = textBox2.Text;
                foreach (LoginInfo item in array)
                {
                    if (item.Email!=name||item.Password!=pwd)
                    {
                        MessageBox.Show("用户名或密码不正确请重新输入!");
                        //清空文本的方法
                        foreach (Control item1 in this.Controls)
                        {
                            if (item1 is TextBox)
                            {
                                item1.Text = "";
                            }
                        }
                        return;
                    }
                    else if (item.Email == name && item.Password == pwd)
                    {
                        // button1.Visible = false;
                        FrmMain fm = new FrmMain();
                        fm.textBox1.Text = "欢迎你" + item.Name;
                        fm.Show();
                        //窗体隐藏
                        this.Hide();
                        break;
                    }
                }
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrmRegist fr = new FrmRegist();
            fr.f1 = this;
            fr.Show();
            //登录窗体隐藏
            this.Hide();
            

        }
    }
}
        
这是登录窗体
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;

namespace FrmLogin
{
    public partial class FrmRegist : Form
    {
        public FrmRegist()
        {
            InitializeComponent();
        }
        public FrmLogin f1;
        private void FrmRegist_Load(object sender, EventArgs e)
        {
          
        }

        private void NewMethod()
        {
            LoginInfo l = new LoginInfo();
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("姓名不能为空", "提示");
                this.textBox1.Focus();
                return;
            }
            else if (this.textBox2.Text == "")
            {
                MessageBox.Show("身份证号码不能为空", "提示");
                this.textBox2.Focus();
                return;
            }
            else if (this.textBox3.Text == ""||this.textBox5.Text=="")
            {
                MessageBox.Show("邮箱地址不能为空", "提示");
                this.textBox3.Focus();
                this.textBox5.Focus();

                return;
            }
            else if (this.textBox4.Text == ""||this.textBox6.Text=="")
            {
                MessageBox.Show("密码不能为空", "提示");
                this.textBox4.Focus();
                this.textBox6.Focus();

                return;
            }
            else
            {
                l.Name = this.textBox1.Text;
                l.ID = this.textBox2.Text;
                l.Email = this.textBox3.Text;
                l.Password = this.textBox4.Text;
                MessageBox.Show("恭喜,通过验证", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                for (int i = 0; i < f1.array.Length; i++)
                {
                    if (f1.array[i] == null)
                    {
                        f1.array[i] = l;
                        break;
                    }
                }
                f1.Visible = true;
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewMethod();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                    item.Text = "";
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值