📄 frmemployees.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace Example_2
{
public class frmEmployees : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtFullName;
private System.Windows.Forms.TextBox txtDepartment;
private System.Windows.Forms.TextBox txtSalary;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Label lblFullName;
private System.Windows.Forms.Label lblDepartment;
private System.Windows.Forms.Label lblSalary;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Label lblEmployeeID;
private System.Windows.Forms.TextBox txtEmployeeID;
//Creating the Hashtable
private Hashtable hashEmployees;
//Declaring static variables
public static string employeeID;
public static string employeeName;
public static string employeeDepartment;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnModify;
public static string employeeSalary;
public frmEmployees()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.lblFullName = new System.Windows.Forms.Label();
this.txtFullName = new System.Windows.Forms.TextBox();
this.txtDepartment = new System.Windows.Forms.TextBox();
this.lblDepartment = new System.Windows.Forms.Label();
this.txtSalary = new System.Windows.Forms.TextBox();
this.lblSalary = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSearch = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.lblEmployeeID = new System.Windows.Forms.Label();
this.txtEmployeeID = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnModify = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblFullName
//
this.lblFullName.Location = new System.Drawing.Point(16, 40);
this.lblFullName.Name = "lblFullName";
this.lblFullName.Size = new System.Drawing.Size(87, 18);
this.lblFullName.TabIndex = 0;
this.lblFullName.Text = "姓名:";
//
// txtFullName
//
this.txtFullName.Location = new System.Drawing.Point(104, 40);
this.txtFullName.Name = "txtFullName";
this.txtFullName.Size = new System.Drawing.Size(201, 21);
this.txtFullName.TabIndex = 1;
this.txtFullName.Text = "";
//
// txtDepartment
//
this.txtDepartment.Location = new System.Drawing.Point(104, 72);
this.txtDepartment.Name = "txtDepartment";
this.txtDepartment.Size = new System.Drawing.Size(201, 21);
this.txtDepartment.TabIndex = 2;
this.txtDepartment.Text = "";
//
// lblDepartment
//
this.lblDepartment.Location = new System.Drawing.Point(16, 72);
this.lblDepartment.Name = "lblDepartment";
this.lblDepartment.Size = new System.Drawing.Size(87, 17);
this.lblDepartment.TabIndex = 2;
this.lblDepartment.Text = "部门:";
//
// txtSalary
//
this.txtSalary.Location = new System.Drawing.Point(104, 104);
this.txtSalary.Name = "txtSalary";
this.txtSalary.Size = new System.Drawing.Size(201, 21);
this.txtSalary.TabIndex = 3;
this.txtSalary.Text = "";
//
// lblSalary
//
this.lblSalary.Location = new System.Drawing.Point(16, 104);
this.lblSalary.Name = "lblSalary";
this.lblSalary.Size = new System.Drawing.Size(87, 17);
this.lblSalary.TabIndex = 6;
this.lblSalary.Text = "工资:";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(328, 8);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(86, 24);
this.btnAdd.TabIndex = 8;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(328, 40);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(88, 24);
this.btnSearch.TabIndex = 9;
this.btnSearch.Text = "搜索(&S)...";
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(328, 152);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(88, 24);
this.btnClose.TabIndex = 13;
this.btnClose.Text = "关闭(&C)";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// lblEmployeeID
//
this.lblEmployeeID.Location = new System.Drawing.Point(16, 16);
this.lblEmployeeID.Name = "lblEmployeeID";
this.lblEmployeeID.Size = new System.Drawing.Size(87, 17);
this.lblEmployeeID.TabIndex = 0;
this.lblEmployeeID.Text = "职员编号:";
//
// txtEmployeeID
//
this.txtEmployeeID.Location = new System.Drawing.Point(104, 8);
this.txtEmployeeID.Name = "txtEmployeeID";
this.txtEmployeeID.Size = new System.Drawing.Size(201, 21);
this.txtEmployeeID.TabIndex = 0;
this.txtEmployeeID.Text = "";
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(0, 136);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(464, 8);
this.groupBox1.TabIndex = 14;
this.groupBox1.TabStop = false;
//
// btnModify
//
this.btnModify.Location = new System.Drawing.Point(328, 104);
this.btnModify.Name = "btnModify";
this.btnModify.Size = new System.Drawing.Size(88, 24);
this.btnModify.TabIndex = 15;
this.btnModify.Text = "修改(&M)";
//
// frmEmployees
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(430, 187);
this.Controls.Add(this.btnModify);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.txtSalary);
this.Controls.Add(this.lblSalary);
this.Controls.Add(this.txtDepartment);
this.Controls.Add(this.lblDepartment);
this.Controls.Add(this.txtFullName);
this.Controls.Add(this.lblFullName);
this.Controls.Add(this.lblEmployeeID);
this.Controls.Add(this.txtEmployeeID);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "frmEmployees";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "职员记录";
this.Load += new System.EventHandler(this.frmEmployees_Load);
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new frmEmployees());
}
private void frmEmployees_Load(object sender, System.EventArgs e)
{
//Creating the hashtable object
hashEmployees = new Hashtable();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
//Checking the text of Add button
if( this.btnAdd.Text.Equals("添加(&A)") )
{
this.txtEmployeeID.Enabled = true;
this.txtFullName.Enabled = true;
this.txtDepartment.Enabled = true;
this.txtSalary.Enabled = true;
this.txtEmployeeID.Text = "";
this.txtFullName.Text = "";
this.txtDepartment.Text = "";
this.txtSalary.Text = "";
this.btnAdd.Text = "更新(&U)";
this.txtEmployeeID.Focus();
}
else
{
//Creating object of Employee class
Employee objEmployee = new Employee();
try
{
objEmployee._employeeID = this.txtEmployeeID.Text;
objEmployee._employeeFullName = this.txtFullName.Text;
objEmployee._employeeDepartment = this.txtDepartment.Text;
objEmployee._employeeSalary = double.Parse(this.txtSalary.Text);
hashEmployees.Add(this.txtEmployeeID.Text,objEmployee);
}
//Cating exceptions
catch(NotSupportedException)
{
MessageBox.Show("不能添加项目");
}
catch(Exception ex)
{
MessageBox.Show("不能添加项目至列表\n" +
ex.Message);
}
this.txtEmployeeID.Text="";
this.txtFullName.Text = "";
this.txtDepartment.Text = "";
this.txtSalary.Text = "";
this.txtEmployeeID.Enabled = false;
this.txtFullName.Enabled = false;
this.txtDepartment.Enabled = false;
this.txtSalary.Enabled = false;
this.btnAdd.Text = "添加(&A)";
}
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
this.txtEmployeeID.Text="";
this.txtFullName.Text = "";
this.txtDepartment.Text = "";
this.txtSalary.Text = "";
//Calling the frmSearch form to find the employees details
frmSearch objSearch = new frmSearch(hashEmployees);
objSearch.ShowDialog();
this.txtEmployeeID.Enabled = true;
this.txtFullName.Enabled = true;
this.txtDepartment.Enabled = true;
this.txtSalary.Enabled = true;
//Displaying the values
this.txtEmployeeID.Text= employeeID;
this.txtFullName.Text = employeeName;
this.txtDepartment.Text = employeeDepartment;
this.txtSalary.Text = employeeSalary;
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -