这是我的主要形式课
这是列表类
这是我的序列化课程
有人可以帮帮我吗。我的表单运行得很好,但是在第一次加载程序时,内容(在这种情况下,联系人的信息,即名字,姓氏手机号码)没有立即显示在列表框中。但是,如果我在文本框中输入新的联系人详细信息并按添加按钮,则新的联系人姓名将显示在列表框中,然后还将显示XML文件中所有已保存的联系人姓名。就像DisplayConcatcs()方法仅在按下添加按钮时才起作用。
我希望XML文件的名称在运行时显示在列表框中,谢谢大家的帮助。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Xml; using System.Xml.Serialization; using System.Xml.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace ContactList_AT2Project { public partial class Form1 : Form { public Form1() { InitializeComponent(); SeriList.Deserialize(Contact, "XmlContacts.xml"); } List<ContactInfo> Contact = new List<ContactInfo>(); private void button1_Click(object sender, EventArgs e) { ContactInfo updateInfo = new ContactInfo(); updateInfo.Surname = tbSurname.Text; updateInfo.Firstname = tbFirstname.Text; updateInfo.Address = tbAddress.Text; updateInfo.Suburb = tbSuburb.Text; updateInfo.Postcode = tbPostcode.Text; updateInfo.Phone = tbPhone.Text; updateInfo.Mobile = tbMobile.Text; if (lbContacts.SelectedIndex == -1) { MessageBox.Show("You must select a contact to update"); } else { string selItem = lbContacts.SelectedItem.ToString(); int index = lbContacts.FindString(selItem); Contact[index].Surname = updateInfo.Surname; Contact[index].Firstname = updateInfo.Firstname; Contact[index].Address = updateInfo.Address; Contact[index].Suburb = updateInfo.Suburb; Contact[index].Postcode = updateInfo.Postcode; Contact[index].Phone = updateInfo.Phone; Contact[index].Mobile = updateInfo.Mobile; DisplayContactList(); } } private void Reset() { tbSurname.Text = ""; tbFirstname.Text = ""; tbAddress.Text = ""; tbSuburb.Text = ""; tbPostcode.Text = ""; tbPhone.Text = ""; tbMobile.Text = ""; } private void btnDelete_Click(object sender, EventArgs e) { if (lbContacts.SelectedIndex == -1) { MessageBox.Show("Select a contact to delete"); } else { string selItem = lbContacts.SelectedItem.ToString(); int index = lbContacts.FindString(selItem); Contact.RemoveAt(index); DisplayContactList(); Reset(); } } private void btnAdd_Click(object sender, EventArgs e) { bool hasData = true; ContactInfo newContact = new ContactInfo(); newContact.Surname = tbSurname.Text; newContact.Firstname = tbFirstname.Text; newContact.Address = tbAddress.Text; newContact.Suburb = tbSuburb.Text; newContact.Postcode = tbPostcode.Text; newContact.Phone = tbPostcode.Text; newContact.Mobile = tbMobile.Text; if (String.IsNullOrEmpty(tbSurname.Text)) { MessageBox.Show("Enter a surname"); hasData = false; return; } if (String.IsNullOrEmpty(tbFirstname.Text)) { MessageBox.Show("Enter a firstname"); hasData = false; return; } if (String.IsNullOrEmpty(tbPhone.Text) && (String.IsNullOrEmpty(tbMobile.Text))) { MessageBox.Show("You need to enter on contact number"); hasData = false; return; } bool duplicate = Contact.Exists(x => x.Surname + x.Firstname == tbSurname.Text + tbFirstname.Text); if (hasData && !duplicate) { Contact.Add(newContact); SortList(); Reset(); DisplayContactList(); } else { MessageBox.Show("That name exists already"); } } public void DisplayContactList() { lbContacts.Items.Clear(); foreach (var con in Contact) { lbContacts.Items.Add(con.Surname + ", " + con.Firstname); } } public void SortList() { Contact.Sort(); DisplayContactList(); } private void lbContacts_MouseClick(object sender, MouseEventArgs e) { if (lbContacts.SelectedIndex == -1) { MessageBox.Show("Select a contact name to display information"); } else { string selItem = lbContacts.SelectedItem.ToString(); int index = lbContacts.FindString(selItem); lbContacts.SetSelected(index, true); tbSurname.Text = Contact[index].Surname; tbFirstname.Text = Contact[index].Firstname; tbAddress.Text = Contact[index].Address; tbSuburb.Text = Contact[index].Suburb; tbPhone.Text = Contact[index].Phone; tbMobile.Text = Contact[index].Mobile; } } private void btnSave_Click(object sender, EventArgs e) { SeriList.SerializeObject(Contact, "XmlContacts.xml"); } private void lbContacts_SelectedIndexChanged(object sender, EventArgs e) { } } }
这是列表类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using System.IO; using System.Xml.Serialization; using System.Threading; namespace ContactList_AT2Project { [Serializable()] public class ContactInfo : IComparable<ContactInfo> { [System.Xml.Serialization.XmlElement("Surname")] public string Surname { get; set; } [System.Xml.Serialization.XmlElement("Firstname")] public string Firstname { get; set; } [System.Xml.Serialization.XmlElement("Address")] public string Address { get; set; } [XmlElement("Suburb")] public string Suburb { get; set; } [XmlElement("Postcode")] public string Postcode { get; set; } [XmlElement("Phone")] public string Phone { get; set; } [XmlElement("Mobile")] public string Mobile { get; set; } public int CompareTo(ContactInfo other) { return this.Surname.CompareTo(other.Surname); } } }
这是我的序列化课程
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using System.IO; using System.Xml.Serialization; using System.Threading; namespace ContactList_AT2Project { public static class SeriList { public static void SerializeObject(this List<ContactInfo> list, string filename) { var serializer = new XmlSerializer(typeof(List<ContactInfo>)); using (var stream = File.OpenWrite(filename)) { serializer.Serialize(stream, list); } } public static void Deserialize(this List<ContactInfo> list, string filename) { var serializer = new XmlSerializer(typeof(List<ContactInfo>)); using (var stream = File.OpenRead(filename)) { var other = (List<ContactInfo>)(serializer.Deserialize(stream)); list.Clear(); list.AddRange(other); } } } }
有人可以帮帮我吗。我的表单运行得很好,但是在第一次加载程序时,内容(在这种情况下,联系人的信息,即名字,姓氏手机号码)没有立即显示在列表框中。但是,如果我在文本框中输入新的联系人详细信息并按添加按钮,则新的联系人姓名将显示在列表框中,然后还将显示XML文件中所有已保存的联系人姓名。就像DisplayConcatcs()方法仅在按下添加按钮时才起作用。
我希望XML文件的名称在运行时显示在列表框中,谢谢大家的帮助。
由主持人最后编辑: