📄 personset.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace ExamSystem
{
/// <summary>
/// PersonSet 的摘要说明。
/// </summary>
public class PersonSet : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ContextMenu cm;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox name;
private System.Windows.Forms.TextBox bh;
private System.Windows.Forms.TextBox content;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public string pID = "";
private System.Windows.Forms.ColumnHeader bh_;
private System.Windows.Forms.ColumnHeader name_;
private System.Windows.Forms.ColumnHeader content_;
public OleDbConnection conn = null;
private TextBox t;
public PersonSet(TextBox t)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
conn = new OleDbConnection(Config.connStr);
this.DataBinder();
this.t = t;
}
//绑字
private void DataBinder(){
this.listView1.Items.Clear();
string sql = "select * from PersonColl order by pID desc";
this.conn.Open();
OleDbDataAdapter odp = new OleDbDataAdapter(sql,conn);
DataSet ds = new DataSet();
odp.Fill(ds);
for(int i=0;i<ds.Tables[0].Rows.Count;i++){
DataRow row = ds.Tables[0].Rows[i];
string[] values = new string[3];
values[0] = row["bh"].ToString();
values[1] = row["name"].ToString();
values[2] = row["content"].ToString();
ListViewItem lvi = new ListViewItem(values);
lvi.Tag = row["pID"].ToString();
this.listView1.Items.Add(lvi);
}
this.conn.Close();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PersonSet));
this.listView1 = new System.Windows.Forms.ListView();
this.bh_ = new System.Windows.Forms.ColumnHeader();
this.name_ = new System.Windows.Forms.ColumnHeader();
this.content_ = new System.Windows.Forms.ColumnHeader();
this.cm = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.content = new System.Windows.Forms.TextBox();
this.bh = new System.Windows.Forms.TextBox();
this.name = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.bh_,
this.name_,
this.content_});
this.listView1.ContextMenu = this.cm;
this.listView1.FullRowSelect = true;
this.listView1.Location = new System.Drawing.Point(8, 16);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(488, 224);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseDown);
this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// bh_
//
this.bh_.Text = "编号";
//
// name_
//
this.name_.Text = "姓名";
this.name_.Width = 100;
//
// content_
//
this.content_.Text = "介绍";
this.content_.Width = 300;
//
// cm
//
this.cm.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem4,
this.menuItem5});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "增加应试者";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "删除应试者";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "-";
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.Text = "刷新列表";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listView1);
this.groupBox1.Location = new System.Drawing.Point(8, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(504, 248);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "应试者列表";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.content);
this.groupBox2.Controls.Add(this.bh);
this.groupBox2.Controls.Add(this.name);
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(8, 272);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(504, 160);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "编辑";
//
// content
//
this.content.Location = new System.Drawing.Point(56, 88);
this.content.Multiline = true;
this.content.Name = "content";
this.content.Size = new System.Drawing.Size(432, 64);
this.content.TabIndex = 5;
this.content.Text = "";
//
// bh
//
this.bh.Location = new System.Drawing.Point(56, 56);
this.bh.Name = "bh";
this.bh.Size = new System.Drawing.Size(264, 21);
this.bh.TabIndex = 4;
this.bh.Text = "";
//
// name
//
this.name.Location = new System.Drawing.Point(56, 24);
this.name.Name = "name";
this.name.Size = new System.Drawing.Size(264, 21);
this.name.TabIndex = 3;
this.name.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 96);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "介绍";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "编号";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "姓名";
//
// button1
//
this.button1.Location = new System.Drawing.Point(320, 440);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 3;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(384, 440);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 4;
this.button2.Text = "取消";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(448, 440);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 5;
this.button3.Text = "关闭";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// PersonSet
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 477);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "PersonSet";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "应试人员设置";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e) {
if(this.name.Text == ""){
MessageBox.Show("应试者姓名不能为空!");
return;
}
string sql = "";
if(pID != ""){
sql = "update PersonColl set name = '"+ this.name.Text +"',bh='"+ this.bh.Text +"',content='"+ this.content.Text +"' where pID="+ this.pID;
}else{
sql = "insert into PersonColl (name,bh,content) values ('"+ this.name.Text +"','"+ this.bh.Text +"','"+ this.content.Text +"')";
}
this.conn.Open();
OleDbCommand odc = new OleDbCommand(sql,conn);
odc.ExecuteNonQuery();
this.conn.Close();
this.DataBinder();
this.pID = "";
this.groupBox2.Text = "新增应试者";
}
private void menuItem5_Click(object sender, System.EventArgs e) {
this.DataBinder();
}
//
private void ClearForm(){
this.name.Text = "";
this.bh.Text = "";
this.content.Text = "";
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e) {
if(this.listView1.Items.Count > 0 && this.listView1.SelectedItems.Count > 0){
this.pID = this.listView1.Items[this.listView1.SelectedItems[0].Index].Tag.ToString();
ListViewItem lvi = this.listView1.Items[this.listView1.SelectedItems[0].Index];
this.name.Text = lvi.SubItems[1].Text.ToString();
this.bh.Text = lvi.SubItems[0].Text.ToString();
this.content.Text = lvi.SubItems[2].Text.ToString();
this.groupBox2.Text = "修改应试者";
this.menuItem1.Enabled = true;
}else{
this.pID = "";
this.menuItem3.Enabled = false;
}
}
/// <summary>
/// 删除应试者
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem3_Click(object sender, System.EventArgs e) {
if(this.pID != ""){
string sql = "delete from PersonColl where pID=" + this.pID;
this.conn.Open();
OleDbCommand odc = new OleDbCommand(sql,conn);
odc.ExecuteNonQuery();
this.conn.Close();
this.DataBinder();
this.ClearForm();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem1_Click(object sender, System.EventArgs e) {
this.groupBox2.Text = "新增应试者";
this.ClearForm();
}
private void button2_Click(object sender, System.EventArgs e) {
this.groupBox2.Text = "新增应试者";
this.ClearForm();
}
private void button3_Click(object sender, System.EventArgs e) {
this.Close();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_DoubleClick(object sender, System.EventArgs e) {
}
private void listView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
if(this.listView1.Items.Count > 0 && this.listView1.SelectedItems.Count > 0 && e.Clicks == 2 && t != null){
ListViewItem lvi = this.listView1.Items[this.listView1.SelectedItems[0].Index];
this.t.Text = lvi.SubItems[1].Text.ToString();
this.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -