/// <summary> /// Handles the "Save Current Format..." context menu click. /// Processes the save to the configuration file then re-loads the list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveNewFormat_Click(object sender, System.EventArgs e) { if (!Directory.Exists(Path.GetDirectoryName(this.savedFormatConfigFile))) { Directory.CreateDirectory(Path.GetDirectoryName(this.savedFormatConfigFile)); } NewFormatForm frmNew = new NewFormatForm(this.rtbFormat.Text); if (DialogResult.OK == frmNew.ShowDialog()) { Format frmt = new Format(); frmt.Name = frmNew.Description; frmt.Value = frmNew.Format; frmt.Delimiter = ddDelimiter.Text; if (this.formats != null) { ArrayList lst = new ArrayList(this.formats.Items); lst.Add(frmt); Format[] newlist = new Format[lst.Count]; lst.CopyTo(newlist); this.formats.Items = newlist.ToList(); } else { this.formats = new StringManipulatorCfg(); this.formats.Items.Add(frmt); } System.Xml.XmlTextWriter tw = null; try { XmlSerializer xmlS = new XmlSerializer(typeof(StringManipulatorCfg)); tw = new System.Xml.XmlTextWriter(this.savedFormatConfigFile, Encoding.UTF8); tw.Indentation = 4; tw.Formatting = System.Xml.Formatting.Indented; xmlS.Serialize(tw, this.formats); tw.Close(); LoadSavedFormats(); } finally { if (tw != null) { tw.Close(); } } } }
/// <summary> /// Gets any pre-saved formats /// </summary> private void LoadSavedFormats() { if (File.Exists(savedFormatConfigFile)) { using (StreamReader sr = new StreamReader(savedFormatConfigFile)) { XmlSerializer serializer = new XmlSerializer(typeof(StringManipulatorCfg)); object obj = serializer.Deserialize(sr); this.formats = (StringManipulatorCfg)obj; } } ctxFormats.Items.Clear(); if (formats != null) { string tease = string.Empty; for (int i = 0; i < formats.Items.Count; i++) { if (formats.Items[i].Value.Length > 30) { tease = formats.Items[i].Value.Substring(0, 30) + " ..."; } else { tease = formats.Items[i].Value; } ToolStripMenuItem item = new ToolStripMenuItem(formats.Items[i].Name + " :: " + tease, null, new EventHandler(InsertFormat_Click)); item.Tag = formats.Items[i]; this.ctxFormats.Items.Add(item); } } if (this.ctxFormats.Items.Count > 0) { this.ctxFormats.Items.Add("-"); } this.ctxFormats.Items.AddRange(SaveNewFormatMenuItem()); }