private void frmMain_Load(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; try { this.tvControllers.SuspendLayout(); m_settings = UserSettings.LoadFromFile(); m_settings.UserSettingsChanged += delegate(object obj, EventArgs ea) { this.lblStatus.Text = m_settings.ToString(); }; this.lblStatus.Text = m_settings.ToString(); m_keypads = MatrixKeypad.LoadIRCsFromFile(); if (m_keypads.Count > 0) { foreach (string s in m_keypads.Keys) { this.tvControllers.Nodes.Add(new TreeNode(s)); } } } catch (Exception ex) { MessageBox.Show("加载配置文件和控制器信息失败,错误消息为:" + ex.Message); } finally { this.tvControllers.ResumeLayout(); this.Cursor = Cursors.Default; } }
private void tvControllers_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (string.IsNullOrWhiteSpace(e.Label)) { e.CancelEdit = true; MessageBox.Show("矩阵键盘名称不能为空或者空格,请重新填写"); e.Node.BeginEdit(); return; } if (e.Label.IndexOfAny(System.IO.Path.GetInvalidFileNameChars(), 0) >= 0) { e.CancelEdit = true; MessageBox.Show("矩阵键盘名称不能有非法字符,请重新填写"); e.Node.BeginEdit(); return; } if (m_keypads.ContainsKey(e.Label)) { e.CancelEdit = true; MessageBox.Show(string.Format("矩阵键盘名称{0}已经存在,请重新填写", e.Node.Text)); e.Node.BeginEdit(); return; } MatrixKeypad irc = new MatrixKeypad(); irc.Name = e.Label; m_keypads.Add(e.Label, irc); this.tvControllers.SelectedNode = e.Node; e.Node.EndEdit(false); this.tvControllers.LabelEdit = false; }
public static Dictionary <string, MatrixKeypad> LoadIRCsFromFile() { System.IO.FileStream fs = null; try { string dirPath = Path.Combine(Application.StartupPath, DirName); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string fileName = string.Format("*{0}", FileExtention); string[] files = Directory.GetFiles(dirPath, fileName); if (files == null || files.Length <= 0) { return(new Dictionary <string, MatrixKeypad>()); } Dictionary <string, MatrixKeypad> results = new Dictionary <string, MatrixKeypad>(); foreach (string s in files) { fs = new System.IO.FileStream(s, System.IO.FileMode.Open, System.IO.FileAccess.Read); XmlSerializer xs = new XmlSerializer(typeof(MatrixKeypad)); MatrixKeypad irc = (MatrixKeypad)xs.Deserialize(fs); results.Add(irc.Name, irc); fs.Close(); } return(results); } catch (Exception ex) { throw new Exception("序列化到文件失败,错误消息为:" + ex.Message, ex); } finally { if (fs != null) { fs.Close(); } } }