学到集合中的HashTable的时候,做一个题目总是有错误,今天总算解决了,好爽。虽然只是小小问题一个。
当我添加信息时,把姓名加到右边的lstBox里,并且以姓名做为键,其他三项保存在一个Employee类中,以这个类的实例作为值将这三项数据保存在一个HashTable中,在"修改"和"删除"的时候,ListBox中的相应信息和HashTable中相应信息都会改动。用来存储信息的类Employee: class Employee { private string _empNo; private string _Name; private string _Department; private float _Salary;
public Employee(string No, string EName, string Depart, float Salary) { this._empNo = No; this._Name = EName; this._Department = Depart; this._Salary = Salary; } public string EmpNo { get { return _empNo; } } public string Name { get { return this._Name; } } public string Department { get { return _Department; } } public float Salray { get { return this._Salary; } } }当ListBox中选择项改变的时候的事件: private void lstShow_SelectedIndexChanged(object sender, EventArgs e) { try { IDictionaryEnumerator myhash = _HashCode.GetEnumerator(); if (this.lstShow.SelectedIndex == -1) { this.txtempNo.Text = ""; this.txtName.Text = ""; this.txtDepart.Text = ""; this.txtSalary.Text = ""; } else { while (myhash.MoveNext()) { Employee emp = (Employee)_HashCode[myhash.Key]; if (lstShow.SelectedItem.ToString() == emp.Name) { this.txtempNo.Text = emp.EmpNo; this.txtName.Text = emp.Name; this.txtDepart.Text = emp.Department; this.txtSalary.Text = emp.Salray.ToString(); }
} }
} catch (InvalidOperationException i) { MessageBox.Show(i.Message); }
}
我的“添加”按钮事件: private void btnAdd_Click(object sender, EventArgs e) { if (this.txtempNo.Text == "" || this.txtempNo.Text == null) { MessageBox.Show("请输入职工编号!"); this.txtempNo.Focus(); return; } else if (this.txtName.Text == "" || this.txtName.Text == null) { MessageBox.Show("请输入姓名!"); this.txtName.Focus(); return; } else if (this.txtDepart.Text == "" || this.txtDepart.Text == null) { MessageBox.Show("请输入部门!"); this.txtDepart.Focus(); return; } else if (this.txtSalary.Text == ""|| float.Parse(this.txtSalary.Text) < 0) { MessageBox.Show("未输入工资或工资小于0!"); this.txtSalary.Focus(); return; } else { try { this._HashCode.Add(this.txtName.Text, new Employee(this.txtempNo.Text, this.txtName.Text, this.txtDepart.Text, float.Parse(this.txtSalary.Text))); this.lstShow.Items.Add(this.txtName.Text); } catch (Exception ne) { MessageBox.Show(ne.Message); } } }
“修改”按钮的事件:private void button1_Click(object sender, EventArgs e) { if (lstShow.SelectedIndex == -1) { MessageBox.Show("请选择要修改的信息!"); return; } try { int index = lstShow.SelectedIndex; this._HashCode.Remove(lstShow.SelectedItem.ToString()); lstShow.Items.RemoveAt(index); lstShow.Items.Insert(index, this.txtName.Text); this._HashCode.Add(name, new Employee(this.txtempNo.Text,this.txtName.Text,this.txtDepart.Text,float.Parse(this.txtSalary.Text))); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
“删除”按钮的事件:private void btnDelete_Click(object sender, EventArgs e) { IDictionaryEnumerator myhash = _HashCode.GetEnumerator(); if (lstShow.SelectedIndex == -1) { MessageBox.Show("请选择要删除的信息!"); return; } try { while (myhash.MoveNext()) { Employee employee = (Employee)_HashCode[myhash.Key]; if (lstShow.SelectedItem.ToString() == employee.Name) { this._HashCode.Remove(lstShow.SelectedItem.ToString()); lstShow.Items.RemoveAt(lstShow.SelectedIndex); } } } catch { MessageBox.Show("成功删除。"); } }
“关闭”按钮的事件:private void btnClose_Click(object sender, EventArgs e) { this.Close(); }
我要说的就是“修改”按钮的事件有错误: 先把选定的从listBox 和HashTable中删除,后面我又从TextBox中读取修改后的数据,而这时候不管是在listBox和HashTable中都不存在这条数据了。
在删除之前把修改的数据保存起来就可以解决:“修改”按钮事件改为: private void button1_Click(object sender, EventArgs e) { string number; string name; string department; float salary; if (lstShow.SelectedIndex == -1) { MessageBox.Show("请选择要修改的信息!"); return; } try { number = this.txtempNo.Text; name = this.txtName.Text; department = this.txtDepart.Text; salary = float.Parse(this.txtSalary.Text); int index = lstShow.SelectedIndex; this._HashCode.Remove(lstShow.SelectedItem.ToString()); lstShow.Items.RemoveAt(index); lstShow.Items.Insert(index, name); this._HashCode.Add(name, new Employee(number, name, department, salary)); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
还有一个限制工资文本框只能输入数字的键盘事件: private void txtSalary_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)8 && e.KeyChar != (char)46) e.Handled = true; }
感觉这种方法很笨,你有好点的方法吗?有的话不要吝啬啦!这样做的话,保存在HashTable中的数据存储的顺序就改变了。