/// <summary> /// Initialize the form /// </summary> /// <param name="cl">Reference to the contactlist</param> public ContactForm(ref ContactList cl) { InitializeComponent(); mailError = new ToolTip(); btnSave.Enabled = false; this.contactList = cl; }
/// <summary> /// Start the form with information from a contact /// Disable textchange eventhandler for txtId to avoid NullReferenceException /// </summary> /// <param name="newContact">Contact with information to be entered in textboxes</param> /// <param name="cl">Reference to the contactlist</param> public ContactForm(Contact newContact, ref ContactList cl) { InitializeComponent(); txtName.Text = newContact.Name; txtId.TextChanged -= txtId_TextChanged; txtId.Text = newContact.Id; txtId.TextChanged += txtId_TextChanged; txtTelPriv.Text = newContact.TelNrPriv; txtMailPriv.Text = newContact.MailPriv; txtMailWork.Text = newContact.MailWork; this.contactList = cl; }
/// <summary> /// Read information from XML-file, add to and return ListView /// </summary> /// <param name="cl">Whole contactlist</param> /// <param name="lv">ListView in mainform</param> /// <param name = "file">Path of the file containing contacts</param> /// <returns>A ListView containing the contacts</returns> public static ListView readFile(ref ContactList cl, ref ListView lv, string file) { XmlTextReader xRead = new XmlTextReader(file); XmlDocument doc = new XmlDocument(); XmlNodeList name = doc.GetElementsByTagName("name"); XmlNodeList id = doc.GetElementsByTagName("id"); XmlNodeList phonepriv = doc.GetElementsByTagName("telnrpriv"); XmlNodeList phonework = doc.GetElementsByTagName("telnrwork"); XmlNodeList mailpriv = doc.GetElementsByTagName("mailpriv"); XmlNodeList mailwork = doc.GetElementsByTagName("mailwork"); string[] post = new string[6]; ListViewItem lvi; try { xRead.WhitespaceHandling = WhitespaceHandling.None; doc.Load(xRead); } catch (Exception e) { MessageBox.Show("Error reading XML-file:\n" + e); } for (int i = 0; i < name.Count; i++) { post[0] = name[i].InnerText; post[1] = id[i].InnerText; post[2] = phonepriv[i].InnerText.Equals("") ? "0" : phonepriv[i].InnerText; post[3] = phonework.Count == 0 ? "" : phonework[i].InnerText; post[4] = mailpriv.Count == 0 ? "" : mailpriv[i].InnerText; post[5] = mailwork.Count == 0 ? "" : mailwork[i].InnerText; lvi = new ListViewItem(post); lvi.ToolTipText = "Doubleclick to change"; lv.Items.Add(lvi); cl.addcontact(new Contact(post[0], post[1], post[2], post[3], post[4], post[5])); } xRead.Close(); return lv; }
/// <summary> /// Save contactlist to a contactfile /// </summary> /// <param name="cl">Whole contactlist</param> /// <param name="filetype">What type of file to write the contacts to (0 = VCard, 1 = CSV)</param> public static void saveToContactFile(ref ContactList cl, int filetype) { SaveFileDialog fileDialog = new SaveFileDialog(); string filename = "Contacts"; fileDialog.InitialDirectory = "H:\\"; if (cl.contactlist.Count == 1) filename = cl.contactlist[0].Name; if (filetype == 1) { fileDialog.DefaultExt = "*.csv"; fileDialog.FileName = filename + ".csv"; } else if (filetype == 0) { fileDialog.FileName = filename + ".vcard"; } if (fileDialog.ShowDialog() == DialogResult.OK) { try { File.WriteAllLines(fileDialog.FileName, cl.getStrings(filetype)); } catch (Exception e) { MessageBox.Show("Error writing contactsfile:\n" + e); } } }
/// <summary> /// Fetch all information in contactlist and write to XML-file /// </summary> /// <param name="cl">Whole contactlist</param> public static void saveToXML(ref ContactList cl) { var writer = new StreamWriter("Telnr.xml", false, System.Text.UnicodeEncoding.Unicode); try { writer.WriteLine("<?xml version=\"1.0\"?>\r\n<contacts>"); foreach (Contact item in cl.contactlist) { writer.WriteLine(item.getXmlString()); } writer.WriteLine("</contacts>"); writer.Close(); } catch (Exception e) { MessageBox.Show("Error when writing XML-file:\n" + e.Message, "", MessageBoxButtons.OK); } return; }
/// <summary> /// Saves the contacts selected in the listview /// Asks what type of contactfile /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void menuSaveSelected_Click(object sender, EventArgs e) { int filetype; FileTypeForm ft = new FileTypeForm(); DialogResult answer = ft.ShowDialog(); if (answer == DialogResult.OK) { string[] selectedRows; ContactList selectedContacts = new ContactList(); filetype = ft.getFileType(); foreach (ListViewItem item in listContacts.SelectedItems) { selectedRows = new string[listContacts.SelectedItems.Count]; selectedContacts.addcontact(item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text, item.SubItems[4].Text, item.SubItems[5].Text); } FileFunctions.saveToContactFile(ref selectedContacts, filetype); } contactlist.Updated = false; }