文章目录
今天我们来用泛型List来实现窗体登录程序,模板和之前的差不多,但是具体代码实现过程有些不同。
一、配置窗体控件布局和源代码
该登录程序包含了5个窗体,分别为:Form1——登录界面、Form2——注册界面、Form3——主界面、Form4——修改密码界面、Form5——账号注销界面。
注意:由于是通过泛型List来实现账号密码的存储,这5个窗体之间数据相互传递、相互关联,如果只截取一段窗体代码运行,则会直接报错。
这次和之前不同,我们先来设置一下注册界面Form2的控件布局和源代码,这是第一步,也是关键的一步。
1、窗体Form2:账号注册界面
label1——新用户名,label2——用户密码,label3——确认密码
textBox1(对应新用户名),textBox2(对应用户密码),textBox3(对应确认密码)
button1——确认,button2——重置,button3——返回
窗体Form2的控件布局
窗体Form2源代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT22
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.Text = "注册界面";
textBox2.PasswordChar = '*';//密码显示为"*"
textBox3.PasswordChar = '*';
ScreenSize();
}
private void Form2_Load(object sender, EventArgs e)
{
ScreenSize();
this.ActiveControl = textBox1;
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button2_Click(object sender, EventArgs e)//重置输入信息
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
this.ActiveControl = textBox1;
}
private void Button3_Click(object sender, EventArgs e)//返回登录界面Form1
{
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
public static List<string> username = new List<string>();
//创建一个公共的泛型List:username,存储输入的账号,作为参数在各窗体之间传递。
public static List<string> password = new List<string>();
//创建一个公共的泛型List:password,存储输入的密码,作为参数在各窗体之间传递。
public static List<string> userpwd = new List<string>();
//创建一个公共的泛型List:userpwd,存储输入的账号+密码,作为参数在各窗体之间传递,同时也是为了验证账号和密码是否匹配。
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("注册账号不得为空!", "提示");
this.ActiveControl = textBox1;
}
else if (textBox2.Text == "")
{
MessageBox.Show("账号密码不得为空!", "提示");
this.ActiveControl = textBox2;
}
else if (textBox3.Text == "" || textBox2.Text != textBox3.Text)
{
MessageBox.Show("请重新确认密码!", "提示");
textBox3.Text = "";
this.ActiveControl = textBox3;
}
/*else if(textBox1.Text.Length<8)
{
MessageBox.Show("请输入8位以上的新用户名!", "提示");
textBox1.Text = "";
}
else if (Regex.IsMatch(textBox2.Text.ToString(), @"^\w+_$") == false)
{
MessageBox.Show("请输入8~16位由字母、数字和下划线组成的密码!");
textBox2.Text = "";
textBox3.Text = "";
}
else if(textBox2.Text.Length<8||textBox2.Text.Length>16)
{
MessageBox.Show("请输入8~16位由字母、数字和下划线组成的密码!");
textBox2.Text = "";
textBox3.Text = "";
}*/
else if (username.Contains(textBox1.Text))
{
MessageBox.Show("该账号已注册!", "提示");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
MessageBox.Show("账号注册成功!", "提示");
username.Add(textBox1.Text); //存储输入的账号
password.Add(textBox2.Text); //存储输入的密码
userpwd.Add(textBox1.Text + textBox2.Text); //存储输入的(账号+密码)
this.Hide(); //隐藏当前窗口,不能用close(),
Form1 sd = new Form1(); //跳转窗体Fomr1
sd.Show();
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
//关闭程序,中断所有进程
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox3.PasswordChar = '\0';
}
else
{
textBox3.PasswordChar = '*';
}
}
}
}
2、窗体Form1:账号登录界面
label1——账号,label2——密码,
textBox1——对应账号输入,textBox2——对应密码输入,
button1——登录,button2——重置,
button3——退出,button4——注册。
checkBox1——是否显示密码。
窗体Form1的控件布局:
窗体Form1源代码
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "登录界面";
textBox2.PasswordChar = '*';
ScreenSize();
}
private void Form1_Load(object sender, EventArgs e)
{
ScreenSize();
this.ActiveControl = textBox1; //鼠标光标在控件textBxo1上面
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
private void Button3_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill(); //退出程序,关闭所有后台线程
}
private void Button4_Click(object sender, EventArgs e)
{
this.Hide();
Form2 sd = new Form2();
sd.Show();
}
public static string username; //设置一个在其他窗体之间传递的参数username,
public static string password; //设置一个在其他窗体之间传递的参数password
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("账号不得为空!", "提示");
this.ActiveControl = textBox1;
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不得为空!", "提示");
this.ActiveControl = textBox2;
}
else if (!Form2.username.Contains(textBox1.Text))
{
DialogResult st = MessageBox.Show("账号不存在,是否选择注册一个新账号?", "提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (st == DialogResult.OK)
{
this.Hide();
Form2 er = new Form2();
er.Show();
}
else
{
textBox1.Text = "";
textBox2.Text = "";
this.ActiveControl = textBox1;
}
}
else if (Form2.userpwd.Contains(textBox1.Text + textBox2.Text))
{
MessageBox.Show("账号登录成功!", "提示");
username = textBox1.Text; //参数username接收输入的账号,
password = textBox2.Text; //参数password接收输入的密码
this.Hide();
Form3 sd = new Form3();
sd.Show();
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox2.Text = "";
this.ActiveControl = textBox2;
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox2.PasswordChar = '\0';
}
else
{
textBox2.PasswordChar = '*';
}
}
}
}
3、窗体Form3:主界面
label1——当前账号,textBox1——显示当前账号,
Button1——修改密码,Button2——注销账号,
Button4——返回,Button3——退出
窗体Form3的控件布局
窗体Form3的源代码
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT22
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.Text = "主界面";
ScreenSize();
}
private void Form3_Load(object sender, EventArgs e)
{
ScreenSize();
this.textBox1.Text = Form1.username; //接收登录界面Form1的参数username.
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button4_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("是否返回登录界面?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (dr == DialogResult.OK)
{
this.Hide();
Form1 er = new Form1();
er.Show();
}
}
private void Button1_Click(object sender, EventArgs e)
{
this.Hide();
Form4 sd = new Form4(); //跳转修改密码界面Form4
sd.Show();
}
private void Button2_Click(object sender, EventArgs e)
{
this.Hide();
Form5 sd = new Form5(); //跳转账号注销界面Form5
sd.Show();
}
private void Button3_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
4、窗体Form4:修改密码界面
label1——旧密码,label2——新密码
textBox1(对应旧密码),textBox2(对应新密码)
Button1——确认,Button2——重置,Button3——取消
窗体Form4的控件布局
窗体Form4源代码
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace CT22
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
this.Text = "修改密码";
ScreenSize();
}
private void Form4_Load(object sender, EventArgs e)
{
ScreenSize();
this.ActiveControl = textBox1;
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
private void Button3_Click(object sender, EventArgs e)
{
this.Hide();
Form3 sd = new Form3();
sd.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入账号密码!", "提示");
this.ActiveControl = textBox1;
}
else if (textBox2.Text == "")
{
MessageBox.Show("请输入新密码!", "提示");
}
else if (Form2.userpwd.Contains(Form1.username + textBox1.Text))
{
MessageBox.Show("账号密码修改成功!请重新登录!");
for (int i = 0; i < Form2.password.Count; i++) //删除旧密码
{
if (textBox1.Text == Form2.password[i])
Form2.password.Remove(Form2.password[i]);
}
for (int j = 0; j < Form2.userpwd.Count; j++) //删除账号+旧密码
{
if (Form1.username + textBox1.Text == Form2.userpwd[j])
Form2.userpwd.Remove(Form2.userpwd[j]);
}
Form2.userpwd.Add(Form1.username + textBox2.Text);
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
else
{
MessageBox.Show("账号密码错误!", "提示");
textBox1.Text = "";
textBox2.Text = "";
this.ActiveControl = textBox1;
}
}
private void Form4_FormClosed(object sender, FormClosedEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void Form4_FormClosing(object sender, FormClosingEventArgs e)
{
Process.GetCurrentProcess().Kill();
}
}
}
5、窗体Form5:账号注销界面
label1——账号密码,textBox1——对应当前账号密码,
button1——注销,button2——重置,button3——取消
窗体Form5的控件布局
窗体Form5源代码
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CT22
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
ScreenSize();
this.Text = "注销界面";
}
private void Form5_Load(object sender, EventArgs e)
{
ScreenSize();
this.ActiveControl = textBox1;
}
public void ScreenSize() //设置窗体样式和居中显示
{
int x = Screen.GetBounds(this).Width;
int y = Screen.GetBounds(this).Height;
this.MinimumSize = new Size(x / 2, y / 2);
this.MaximumSize = new Size(x / 2, y / 2);
this.CenterToParent();
this.CenterToScreen();
}
private void Button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
this.ActiveControl = textBox1;
}
private void Button3_Click(object sender, EventArgs e)
{
this.Hide();
Form3 sd = new Form3();
sd.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入账号密码!", "提示");
this.ActiveControl = textBox1;
}
else if (Form2.userpwd.Contains(Form1.username + textBox1.Text))
{
DialogResult st = MessageBox.Show("是否要注销账号?", "提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
if (st == DialogResult.OK)
{
MessageBox.Show("账号注销成功!", "提示");
for (int i = 0; i < Form2.username.Count; i++)
{
if (Form1.username.Equals(Form2.username[i]))
Form2.username.Remove(Form2.username[i]);
}
for (int i = 0; i < Form2.password.Count; i++)
{
if (textBox1.Text.Equals(Form2.password[i]))
Form2.password.Remove(Form2.password[i]);
}
for (int i = 0; i < Form2.userpwd.Count; i++)
{
if ((Form1.username + textBox1.Text).Equals(Form2.userpwd[i]))
Form2.userpwd.Remove(Form2.userpwd[i]);
}
this.Hide();
Form1 sd = new Form1();
sd.Show();
}
else
{
textBox1.Text = "";
this.ActiveControl = textBox1;
}
}
}
}
}
二、总结
以上就是今天要跟大家分享的内容,之前我用C#窗体连接数据库来实现账号登录和注册(点击即可查看),但是这次的登录程序没有连接数据库,而是通过创建泛型List来存储账号密码,因为在之前的文章(点击即可查看)中我也提过,只要找到存储数据的途径,剩下的就好办了。
个人声明:以上内容未经本人允许禁止转载!!!如果要引用部分代码,请标明出处!!