/// <summary> /// 数据添加按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDataAdd_Click(object sender, EventArgs e) { if (tBMail.Text == "" || tBName.Text == "" || tBAge.Text == "") { MessageBox.Show("输入完整信息"); } Person p = new Person(tBName.Text, cBSex.SelectedItem.ToString(), tBMail.Text, int.Parse(tBAge.Text)); lBData.Items.Add(tBName.Text); date.Add(p); }
/// <summary> /// 数据载入,窗口初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { cBSex.SelectedIndex = 0; if(!File.Exists("人员.txt")) return; using (StreamReader sr = new StreamReader("人员.txt",Encoding.Default)) { string str = null; string[] strs = null; while (!sr.EndOfStream) { str = sr.ReadLine(); strs = str.Split(','); Person p = new Person(strs[0], strs[1], strs[3], int.Parse(strs[2])); date.Add(p); lBData.Items.Add(strs[0]); //lBData.Items自动使用Tostring(),所以会显示命名空间,所以可以重写Person.Tostring() //这里就能直接把Person对象保存在Items中,且正确的显示姓名 } } }