private void MenuClicked(Object sender, EventArgs e) //分组操作——右键菜单点击事件 { TreeNode current_node = tree_devices.SelectedNode; CustomNode node = (CustomNode)current_node.Tag; string addr = node.Addr; XmlElement list = document.DocumentElement; XmlNodeList groups = list.ChildNodes; XmlElement child = null; foreach (XmlElement group in groups) { foreach (XmlElement fan in group) { if (fan.ChildNodes[0].InnerText == addr) { child = fan; } } } ToolStripMenuItem item = (ToolStripMenuItem)sender; foreach (XmlElement group in groups) { string name = group.GetAttribute("name"); if (name.Equals(item.Text)) { group.AppendChild(child); } } document.Save(Application.StartupPath + "\\DeviceName.xml"); Refresh_GroupList(); }
private void Refresh_GroupList() //刷新分组列表 { XmlElement list = document.DocumentElement; XmlNodeList group = list.ChildNodes; TreeNode root = tree_devices.Nodes[0]; root.Nodes.Clear(); foreach (XmlElement gp in group) { string gp_name = gp.GetAttribute("name"); TreeNode[] children = new TreeNode[gp.ChildNodes.Count]; for (int i = 0; i < gp.ChildNodes.Count; i++) { XmlNode fan = gp.ChildNodes.Item(i); CustomNode node = new CustomNode(); node.Addr = fan.FirstChild.InnerText; node.DeviceID = fan.ChildNodes[1].InnerText; node.Name = fan.LastChild.InnerText; TreeNode newFan = new TreeNode(node.Name); newFan.Tag = node; children[i] = newFan; } TreeNode newGroup = new TreeNode(gp_name, children); root.Nodes.Add(newGroup); } tree_devices.SelectedNode = root; tree_devices.ExpandAll(); }
private void tree_devices_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode current_node = tree_devices.SelectedNode; this.GroupFanList.Clear(); int level = current_node.Level; switch (level) { case 0: lab_groupName.Text = "所有设备"; lab_groupName.Enabled = false; btn_gn_save.Enabled = false; btn_del_group.Enabled = false; this.GroupFanList = new Dictionary <string, Fan>(this.FanList); Refresh_FanList_Group(this.GroupFanList); break; case 1: lab_groupName.Text = current_node.Text; lab_previous.Text = current_node.Text; if ("未分组".Equals(current_node.Text)) { btn_gn_save.Enabled = false; lab_groupName.Enabled = false; btn_del_group.Enabled = false; } else { btn_gn_save.Enabled = true; lab_groupName.Enabled = true; btn_del_group.Enabled = true; } foreach (TreeNode child in current_node.Nodes) { CustomNode node = (CustomNode)child.Tag; string addr = node.Addr; Fan fan = new Fan(); this.FanList.TryGetValue(addr, out fan); if (fan != null) { this.GroupFanList.Add(addr, fan); } } Refresh_FanList_Group(this.GroupFanList); break; case 2: lab_groupName.Text = current_node.Parent.Text; lab_groupName.Enabled = false; btn_gn_save.Enabled = false; btn_del_group.Enabled = false; CustomNode node_spe = (CustomNode)current_node.Tag; string addr_spe = node_spe.Addr; Fan fan_spe = new Fan(); this.FanList.TryGetValue(addr_spe, out fan_spe); if (fan_spe != null) { this.GroupFanList.Add(addr_spe, fan_spe); } Refresh_FanList_Group(this.GroupFanList); break; } }