用C#开发一个简易的登录注册窗口
主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login
{
struct Information
{
public String name;
public String UseName;
public String password;
public String time;
public Char sex;
public String bent;
}
public class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SignInForm());
}
}
}
登录窗口
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 Login
{
public partial class SignInForm : Form
{
public SignInForm()
{
InitializeComponent();
}
static String UName, password;
public SignInForm(String str1,String str2)
{ //获取用户名密码
InitializeComponent();
UName = str1;
password = str2;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Password_TextChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked)
{
Password.PasswordChar = '*';
}
}
private void button3_Click(object sender, EventArgs e)
{
Form f1 = new SignUpForm();
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
if(this.UseName.Text == UName && this.Password.Text == password)
{
MessageBox.Show("Hello World!"); //登陆成功
}
else
{
MessageBox.Show("用户名或密码错误!");
this.UseName.Clear();
this.Password.Clear();
this.UseName.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
Password.PasswordChar = new char(); //显示密码
else
{
Password.PasswordChar = '*';
}
}
}
}
注册窗口
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 Login
{
public partial class SignUpForm : Form
{
public SignUpForm()
{
InitializeComponent();
}
public void SignUp_Load(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.PasswordChar = '*';
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
textBox4.PasswordChar = '*';
}
public void button1_Click(object sender, EventArgs e)
{
if (textBox3.Text == textBox4.Text)
{
Information information;
information.name = textBox1.Text;
information.UseName = textBox2.Text;
information.password = textBox3.Text;
information.time = dateTimePicker1.Text;
if (radioButton1.Checked)
{
information.sex = '男';
}
else if (radioButton2.Checked)
{
information.sex = '女';
}
information.bent = "";
foreach (Control c in groupBox2.Controls)
{ //获取爱好所选选项
if (c is CheckBox && ((CheckBox)c).Checked == true)
{
information.bent += c.Text + " ";
}
}
MessageBox.Show("注册成功,请返回登录。");
//MessageBox.Show("", information.bent); //测试爱好是否存入
SignInForm name_password = new SignInForm(information.name,information.password); //传递用户名密码到登陆窗口
this.Close();
}
else
{
MessageBox.Show("两次密码不同!");
this.textBox4.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{ //点击取消清除数据
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox3.Clear();
this.textBox4.Clear();
this.textBox1.Focus();
this.radioButton1.Checked = false;
this.radioButton2.Checked = false;
this.checkBox3.Checked = false;
this.checkBox4.Checked = false;
this.checkBox5.Checked = false;
this.checkBox6.Checked = false;
this.checkBox7.Checked = false;
}
}
}