将代码复制之后,可以看到界面. 我的问题是:单击"添加"按纽可以把数据添加到哈希表中,同时姓名添加到右边列表框中.单击"删除"按纽,可以把该职员删除,同时,哈希表中的职员信息也被删除(要求不使用数据库并且调试程序时,使用监视窗口监视哈希表中的数据).
我的代码如下,请大家帮我解决,谢谢了.using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MyPrj
{
public class Form1 : System.Windows.Forms.Form
{
public Hashtable _hashtable=new Hashtable();
private System.Windows.Forms.Label lblId;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblDept;
private System.Windows.Forms.Label lblSar;
private System.Windows.Forms.TextBox txtId;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtDept;
private System.Windows.Forms.TextBox txtSar;
private System.Windows.Forms.ListBox lstName;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <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()
{
this.lblId = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblDept = new System.Windows.Forms.Label();
this.lblSar = new System.Windows.Forms.Label();
this.txtId = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtDept = new System.Windows.Forms.TextBox();
this.txtSar = new System.Windows.Forms.TextBox();
this.lstName = new System.Windows.Forms.ListBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblId
//
this.lblId.Location = new System.Drawing.Point(32, 32);
this.lblId.Name = "lblId";
this.lblId.Size = new System.Drawing.Size(72, 16);
this.lblId.TabIndex = 0;
this.lblId.Text = "职员编号:";
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(32, 72);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(72, 16);
this.lblName.TabIndex = 1;
this.lblName.Text = "姓名:";
//
// lblDept
//
this.lblDept.Location = new System.Drawing.Point(32, 112);
this.lblDept.Name = "lblDept";
this.lblDept.Size = new System.Drawing.Size(72, 16);
this.lblDept.TabIndex = 2;
this.lblDept.Text = "部门:";
//
// lblSar
//
this.lblSar.Location = new System.Drawing.Point(32, 152);
this.lblSar.Name = "lblSar";
this.lblSar.Size = new System.Drawing.Size(72, 16);
this.lblSar.TabIndex = 3;
this.lblSar.Text = "工资:";
//
// txtId
//
this.txtId.Location = new System.Drawing.Point(128, 24);
this.txtId.Name = "txtId";
this.txtId.Size = new System.Drawing.Size(120, 21);
this.txtId.TabIndex = 4;
this.txtId.Text = "";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(128, 64);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(120, 21);
this.txtName.TabIndex = 5;
this.txtName.Text = "";
//
// txtDept
//
this.txtDept.Location = new System.Drawing.Point(128, 104);
this.txtDept.Name = "txtDept";
this.txtDept.Size = new System.Drawing.Size(120, 21);
this.txtDept.TabIndex = 6;
this.txtDept.Text = "";
//
// txtSar
//
this.txtSar.Location = new System.Drawing.Point(128, 144);
this.txtSar.Name = "txtSar";
this.txtSar.Size = new System.Drawing.Size(120, 21);
this.txtSar.TabIndex = 7;
this.txtSar.Text = "";
//
// lstName
//
this.lstName.ItemHeight = 12;
this.lstName.Location = new System.Drawing.Point(280, 24);
this.lstName.Name = "lstName";
this.lstName.Size = new System.Drawing.Size(216, 148);
this.lstName.TabIndex = 8;
//this.lstName.SelectedIndexChanged += new System.EventHandler(this.lstName_SelectedIndexChanged);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(168, 208);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(80, 24);
this.btnAdd.TabIndex = 9;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(328, 208);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(80, 24);
this.btnDel.TabIndex = 10;
this.btnDel.Text = "删除(&R)";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(424, 208);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 24);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "关闭(&C)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 246);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.lstName);
this.Controls.Add(this.txtSar);
this.Controls.Add(this.txtDept);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtId);
this.Controls.Add(this.lblSar);
this.Controls.Add(this.lblDept);
this.Controls.Add(this.lblName);
this.Controls.Add(this.lblId);
this.Name = "Form1";
this.Text = "职员记录";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
_hashtable.Add(txtId.Text,txtName.Text);
}
catch(ArgumentException)
{
MessageBox.Show("不能添加重复的值","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
return;
}
catch(Exception ex)
{
MessageBox.Show("产生系统错误");
Application.Exit();
}
lstName.Items.Add(txtName.Text);
} private void btnDel_Click(object sender, System.EventArgs e)
{
IDictionaryEnumerator myEnumerator=_hashtable.GetEnumerator();
while(myEnumerator.MoveNext())
{
Employee A=(Employee)_hashtable[myEnumerator.Key];
if((String)(lstName.SelectedItem)==A._empName)
{
_hashtable.Remove(A._empName);
lstName.Items.Remove(lstName.SelectedItem);
}
}
}
} public class Employee
{
public string _empId;
public string _empName;
public string _empDept;
public string _empSar; public string Id
{
get
{
return _empId;
}
} public string Name
{
get
{
return _empName;
}
} public string Dept
{
get
{
return _empDept;
}
} public string Sar
{
get
{
return _empSar;
}
}
}
}
我的代码如下,请大家帮我解决,谢谢了.using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MyPrj
{
public class Form1 : System.Windows.Forms.Form
{
public Hashtable _hashtable=new Hashtable();
private System.Windows.Forms.Label lblId;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblDept;
private System.Windows.Forms.Label lblSar;
private System.Windows.Forms.TextBox txtId;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtDept;
private System.Windows.Forms.TextBox txtSar;
private System.Windows.Forms.ListBox lstName;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <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()
{
this.lblId = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblDept = new System.Windows.Forms.Label();
this.lblSar = new System.Windows.Forms.Label();
this.txtId = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtDept = new System.Windows.Forms.TextBox();
this.txtSar = new System.Windows.Forms.TextBox();
this.lstName = new System.Windows.Forms.ListBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblId
//
this.lblId.Location = new System.Drawing.Point(32, 32);
this.lblId.Name = "lblId";
this.lblId.Size = new System.Drawing.Size(72, 16);
this.lblId.TabIndex = 0;
this.lblId.Text = "职员编号:";
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(32, 72);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(72, 16);
this.lblName.TabIndex = 1;
this.lblName.Text = "姓名:";
//
// lblDept
//
this.lblDept.Location = new System.Drawing.Point(32, 112);
this.lblDept.Name = "lblDept";
this.lblDept.Size = new System.Drawing.Size(72, 16);
this.lblDept.TabIndex = 2;
this.lblDept.Text = "部门:";
//
// lblSar
//
this.lblSar.Location = new System.Drawing.Point(32, 152);
this.lblSar.Name = "lblSar";
this.lblSar.Size = new System.Drawing.Size(72, 16);
this.lblSar.TabIndex = 3;
this.lblSar.Text = "工资:";
//
// txtId
//
this.txtId.Location = new System.Drawing.Point(128, 24);
this.txtId.Name = "txtId";
this.txtId.Size = new System.Drawing.Size(120, 21);
this.txtId.TabIndex = 4;
this.txtId.Text = "";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(128, 64);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(120, 21);
this.txtName.TabIndex = 5;
this.txtName.Text = "";
//
// txtDept
//
this.txtDept.Location = new System.Drawing.Point(128, 104);
this.txtDept.Name = "txtDept";
this.txtDept.Size = new System.Drawing.Size(120, 21);
this.txtDept.TabIndex = 6;
this.txtDept.Text = "";
//
// txtSar
//
this.txtSar.Location = new System.Drawing.Point(128, 144);
this.txtSar.Name = "txtSar";
this.txtSar.Size = new System.Drawing.Size(120, 21);
this.txtSar.TabIndex = 7;
this.txtSar.Text = "";
//
// lstName
//
this.lstName.ItemHeight = 12;
this.lstName.Location = new System.Drawing.Point(280, 24);
this.lstName.Name = "lstName";
this.lstName.Size = new System.Drawing.Size(216, 148);
this.lstName.TabIndex = 8;
//this.lstName.SelectedIndexChanged += new System.EventHandler(this.lstName_SelectedIndexChanged);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(168, 208);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(80, 24);
this.btnAdd.TabIndex = 9;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(328, 208);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(80, 24);
this.btnDel.TabIndex = 10;
this.btnDel.Text = "删除(&R)";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(424, 208);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 24);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "关闭(&C)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 246);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.lstName);
this.Controls.Add(this.txtSar);
this.Controls.Add(this.txtDept);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtId);
this.Controls.Add(this.lblSar);
this.Controls.Add(this.lblDept);
this.Controls.Add(this.lblName);
this.Controls.Add(this.lblId);
this.Name = "Form1";
this.Text = "职员记录";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
_hashtable.Add(txtId.Text,txtName.Text);
}
catch(ArgumentException)
{
MessageBox.Show("不能添加重复的值","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
return;
}
catch(Exception ex)
{
MessageBox.Show("产生系统错误");
Application.Exit();
}
lstName.Items.Add(txtName.Text);
} private void btnDel_Click(object sender, System.EventArgs e)
{
IDictionaryEnumerator myEnumerator=_hashtable.GetEnumerator();
while(myEnumerator.MoveNext())
{
Employee A=(Employee)_hashtable[myEnumerator.Key];
if((String)(lstName.SelectedItem)==A._empName)
{
_hashtable.Remove(A._empName);
lstName.Items.Remove(lstName.SelectedItem);
}
}
}
} public class Employee
{
public string _empId;
public string _empName;
public string _empDept;
public string _empSar; public string Id
{
get
{
return _empId;
}
} public string Name
{
get
{
return _empName;
}
} public string Dept
{
get
{
return _empDept;
}
} public string Sar
{
get
{
return _empSar;
}
}
}
}
_hashtable.Add(txtId.Text,txtName.Text); //value是string类型删除时是:
Employee A=(Employee)_hashtable[myEnumerator.Key];类型转型有问题。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace MyPrj
{
public class Form1 : System.Windows.Forms.Form
{
public Hashtable _hashtable=new Hashtable();
private System.Windows.Forms.Label lblId;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lblDept;
private System.Windows.Forms.Label lblSar;
private System.Windows.Forms.TextBox txtId;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtDept;
private System.Windows.Forms.TextBox txtSar;
private System.Windows.Forms.ListBox lstName;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <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()
{
this.lblId = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.lblDept = new System.Windows.Forms.Label();
this.lblSar = new System.Windows.Forms.Label();
this.txtId = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.txtDept = new System.Windows.Forms.TextBox();
this.txtSar = new System.Windows.Forms.TextBox();
this.lstName = new System.Windows.Forms.ListBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblId
//
this.lblId.Location = new System.Drawing.Point(32, 32);
this.lblId.Name = "lblId";
this.lblId.Size = new System.Drawing.Size(72, 16);
this.lblId.TabIndex = 0;
this.lblId.Text = "职员编号:";
//
// lblName
//
this.lblName.Location = new System.Drawing.Point(32, 72);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(72, 16);
this.lblName.TabIndex = 1;
this.lblName.Text = "姓名:";
//
// lblDept
//
this.lblDept.Location = new System.Drawing.Point(32, 112);
this.lblDept.Name = "lblDept";
this.lblDept.Size = new System.Drawing.Size(72, 16);
this.lblDept.TabIndex = 2;
this.lblDept.Text = "部门:";
//
// lblSar
//
this.lblSar.Location = new System.Drawing.Point(32, 152);
this.lblSar.Name = "lblSar";
this.lblSar.Size = new System.Drawing.Size(72, 16);
this.lblSar.TabIndex = 3;
this.lblSar.Text = "工资:";
//
// txtId
//
this.txtId.Location = new System.Drawing.Point(128, 24);
this.txtId.Name = "txtId";
this.txtId.Size = new System.Drawing.Size(120, 21);
this.txtId.TabIndex = 4;
this.txtId.Text = "";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(128, 64);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(120, 21);
this.txtName.TabIndex = 5;
this.txtName.Text = "";
//
// txtDept
//
this.txtDept.Location = new System.Drawing.Point(128, 104);
this.txtDept.Name = "txtDept";
this.txtDept.Size = new System.Drawing.Size(120, 21);
this.txtDept.TabIndex = 6;
this.txtDept.Text = "";
//
// txtSar
//
this.txtSar.Location = new System.Drawing.Point(128, 144);
this.txtSar.Name = "txtSar";
this.txtSar.Size = new System.Drawing.Size(120, 21);
this.txtSar.TabIndex = 7;
this.txtSar.Text = "";
//
// lstName
//
this.lstName.ItemHeight = 12;
this.lstName.Location = new System.Drawing.Point(280, 24);
this.lstName.Name = "lstName";
this.lstName.Size = new System.Drawing.Size(216, 148);
this.lstName.TabIndex = 8;
//this.lstName.SelectedIndexChanged += new System.EventHandler(this.lstName_SelectedIndexChanged);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(168, 208);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(80, 24);
this.btnAdd.TabIndex = 9;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(328, 208);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(80, 24);
this.btnDel.TabIndex = 10;
this.btnDel.Text = "删除(&R)";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(424, 208);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 24);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "关闭(&C)";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 246);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.lstName);
this.Controls.Add(this.txtSar);
this.Controls.Add(this.txtDept);
this.Controls.Add(this.txtName);
this.Controls.Add(this.txtId);
this.Controls.Add(this.lblSar);
this.Controls.Add(this.lblDept);
this.Controls.Add(this.lblName);
this.Controls.Add(this.lblId);
this.Name = "Form1";
this.Text = "职员记录";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
if (txtId.Text.Trim() == "")
{
MessageBox.Show("ID不能为空","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
return;
}
else if (_hashtable.ContainsKey(txtId.Text.Trim()))
{
//尽量不要用异常来处理意料中的情况,有性能影响
MessageBox.Show("不能添加重复的值","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
return;
}
else
{
_hashtable.Add(txtId.Text.Trim(),new Employee(txtId.Text.Trim(),txtName.Text,txtDept.Text,txtSar.Text));
}
}
catch(Exception ex)
{
MessageBox.Show("产生系统错误");
Application.Exit();
}
lstName.Items.Add(txtId.Text);
} private void btnDel_Click(object sender, System.EventArgs e)
{
if (lstName.SelectedItem != null)
{
_hashtable.Remove(lstName.SelectedItem);
lstName.Items.Remove(lstName.SelectedItem);
}
}
} public class Employee
{
public string _empId;
public string _empName;
public string _empDept;
public string _empSar; public Employee(string id,string name,string dept,string sar)
{
this._empId = id;
this._empName = name;
this._empDept = dept;
this._empSar = sar;
} public string Id
{
get
{
return _empId;
}
} public string Name
{
get
{
return _empName;
}
} public string Dept
{
get
{
return _empDept;
}
} public string Sar
{
get
{
return _empSar;
}
}
}
}