📄 kaoqin_person.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace kaoqin.user
{
public partial class kaoqin_person : Form
{
public kaoqin_person()
{
InitializeComponent();
}
private void BindData()
{
this.listBox1.Items.Clear();
string sql = "select * from [user]";
DataTable dt = DB.getTable(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
this.listBox1.Items.Add(dt.Rows[i]["name"].ToString().Trim());
}
}
private void BindText(string name)
{
string sql = "select * from [user] where name='" + name + "'";
DataTable dt = DB.getTable(sql);
this.textBox1.Text = dt.Rows[0]["name"].ToString().Trim();
this.textBox2.Text = dt.Rows[0]["pwd"].ToString().Trim();
this.textBox3.Text = dt.Rows[0]["pwd"].ToString().Trim();
this.textBox4.Text = dt.Rows[0]["realName"].ToString().Trim();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
this.lblFlag.Text = "添加";
this.textBox1.Text = "";
this.groupBox2.Enabled = true;
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
this.textBox4.Text = "";
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
{
this.lblFlag.Text = "删除";
this.groupBox2.Enabled = false;
if (this.listBox1.SelectedIndex == -1)
{
this.textBox1.Text = "";
return;
}
this.BindText(this.listBox1.SelectedItem.ToString().Trim());
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
{
this.lblFlag.Text = "修改";
this.groupBox2.Enabled = true;
if (this.listBox1.SelectedIndex == -1)
{
this.textBox1.Text = "";
return;
}
this.BindText(this.listBox1.SelectedItem.ToString().Trim());
}
else
{
this.groupBox2.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
Add();
}
else if (this.radioButton2.Checked == true)
{
if (MessageBox.Show("你真的想要删除此记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
Del();
}
}
else if (this.radioButton3.Checked == true)
{
Mod();
}
}
private void Add()
{
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("用户名不能为空!");
this.textBox1.Focus();
return;
}
if (this.textBox2.Text.Trim() == "")
{
MessageBox.Show("密码不能为空!");
this.textBox2.Focus();
return;
}
if (this.textBox3.Text.Trim() == "")
{
MessageBox.Show("确认密码不能为空!");
this.textBox3.Focus();
return;
}
if (this.textBox4.Text.Trim() == "")
{
MessageBox.Show("真实姓名不能为空!");
this.textBox4.Focus();
return;
}
if (this.textBox2.Text.Trim() != this.textBox3.Text.Trim())
{
MessageBox.Show("两次密码不一致!");
this.textBox3.Focus();
return;
}
string sql = "select * from [user] where name='" + this.textBox1.Text.Trim() + "'";
DataTable dt = DB.getTable(sql);
if (dt.Rows.Count != 0)
{
MessageBox.Show("考勤人员" + this.textBox1.Text.Trim() + "已存在!");
return;
}
sql = "insert into [user] values ('" + this.textBox1.Text.Trim() + "','" + this.textBox2.Text.Trim() + "','" + this.textBox4.Text.Trim() + "')";
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
this.BindData();
}
else
{
MessageBox.Show("操作失败!");
}
}
private void Mod()
{
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("用户名不能为空!");
this.textBox1.Focus();
return;
}
if (this.textBox2.Text.Trim() == "")
{
MessageBox.Show("密码不能为空!");
this.textBox2.Focus();
return;
}
if (this.textBox3.Text.Trim() == "")
{
MessageBox.Show("确认密码不能为空!");
this.textBox3.Focus();
return;
}
if (this.textBox4.Text.Trim() == "")
{
MessageBox.Show("真实姓名不能为空!");
this.textBox4.Focus();
return;
}
if (this.textBox2.Text.Trim() != this.textBox3.Text.Trim())
{
MessageBox.Show("两次密码不一致!");
this.textBox3.Focus();
return;
}
if (this.listBox1.SelectedIndex == -1)
{
MessageBox.Show("请选择一个用户!");
this.listBox1.Focus();
return;
}
string sql = "select * from [user] where name='" + this.textBox1.Text.Trim() + "'";
DataTable dt = DB.getTable(sql);
if (dt.Rows.Count != 0)
{
MessageBox.Show("考勤人员" + this.textBox1.Text.Trim() + "已存在!");
return;
}
sql = "update [user] set name='" + this.textBox1.Text.Trim() + "',pwd='" + this.textBox2.Text.Trim() + "',realName='" + this.textBox4.Text.Trim() + "' where name='" + this.listBox1.SelectedItem.ToString() + "'";
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
this.BindData();
}
else
{
MessageBox.Show("操作失败!");
}
}
private void Del()
{
if (this.listBox1.SelectedIndex == -1)
{
MessageBox.Show("请选择一个用户!");
this.listBox1.Focus();
return;
}
string sql = "delete [user] where name='" + this.listBox1.SelectedItem.ToString() + "'";
if (DB.executeSql(sql))
{
MessageBox.Show("操作成功!");
this.BindData();
}
else
{
MessageBox.Show("操作失败!");
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true || this.radioButton2.Checked == true)
{
if (this.listBox1.SelectedIndex == -1)
{
return;
}
this.BindText(this.listBox1.SelectedItem.ToString().Trim());
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void kaoqin_person_Load(object sender, EventArgs e)
{
this.BindData();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -