五、功能实现
1、 数据的保存与更新
当新建一个菜单时,其在树中的节点Node(Name=“new_”+GUID);当修改一个菜单时,其在树中的节点Node(Name=”edit_”+GUID)。
在保存时,通过遍历菜单树中的节点Node.Name,根据该字符串的开头调用不同的保存方法。
1 /// <summary> 创建一个TreeNode,定义Tag,Name,Text 2 /// </summary> 3 /// <returns></returns> 4 public static TreeNode CreateTreeNode() 5 { 6 TreeNode node = new TreeNode(); 7 node.Name = "new_" + Guid.NewGuid().ToString(); 8 node.Tag = 0; 9 node.Text = "请输入菜单名称"; 10 return node; 11 }
(2)编辑菜单(略)
2、 快速定位菜单
通过点击TreeView中的某个节点,DataGridView中与之对应的菜单Row高亮。同理,通过选中DataGridView某条菜单Row,TreeView中与之对应的菜单Node高亮。
(1)双击TreeView中的Node
1 /// <summary> 双击TreeView中的某个Node,该node允许编辑,同时dataGridView中该node的记录高亮 2 /// </summary> 3 /// <param name="sender"></param> 4 /// <param name="e"></param> 5 private void treeViewMenu_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) 6 { 7 //获取该节点的ID_ 8 9 int id = Convert.ToInt32(treeViewMenu.SelectedNode.Tag); 10 int index = BLMenu.GetIndexByID(id); 11 dataGridViewMenu.CurrentCell = dataGridViewMenu.Rows[index].Cells[0]; 12 dataGridViewMenu.Rows[index].Selected = true; 13 treeViewMenu.LabelEdit = true; 14 }
(2)选中DataGridView中的某行
1 /// <summary> 选择dataGridView中的某行,使其对应的Node节点高亮 2 /// </summary> 3 /// <param name="sender"></param> 4 /// <param name="e"></param> 5 private void dataGridViewMenu_RowEnter(object sender, DataGridViewCellEventArgs e) 6 { 7 treeViewMenu.Focus(); 8 treeViewMenu.ExpandAll(); 9 int rowid = e.RowIndex; 10 int id = Convert.ToInt32(dataGridViewMenu.Rows[rowid].Cells[9].Value); 11 TreeNode node = searchNodeById(id, treeViewMenu.Nodes); 12 treeViewMenu.SelectedNode = node; 13 }
3、 拖动定位菜单
拖动TreeView中的某个节点,达到快速修改其隶属关系的目的。
1 /// <summary> 在用鼠标将某项拖动到该控件的工作区时发生 2 /// </summary> 3 /// <param name="sender"></param> 4 /// <param name="e"></param> 5 private void treeViewMenu_DragEnter(object sender, DragEventArgs e) 6 { 7 if (e.Data.GetDataPresent(typeof(TreeNode))) 8 e.Effect = DragDropEffects.Move; 9 else 10 e.Effect = DragDropEffects.None; 11 } 12 13 /// <summary> 在用户开始拖动项时发生 14 /// </summary> 15 /// <param name="sender"></param> 16 /// <param name="e"></param> 17 private void treeViewMenu_ItemDrag(object sender, ItemDragEventArgs e) 18 { 19 if (e.Button == MouseButtons.Left) 20 { 21 DoDragDrop(e.Item, DragDropEffects.Move); 22 } 23 } 24 25 /// <summary> 在用户拖动完成时发生 26 /// </summary> 27 /// <param name="sender"></param> 28 /// <param name="e"></param> 29 private void treeViewMenu_DragDrop(object sender, DragEventArgs e) 30 { 31 try 32 { 33 TreeNode currentNode = null, targetNode; 34 if (e.Data.GetDataPresent(typeof(TreeNode))) 35 currentNode = (TreeNode)(e.Data.GetData(typeof(TreeNode))); 36 if (currentNode == null) 37 return; 38 // 找到目标节点 39 Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y)); 40 targetNode = this.treeViewMenu.GetNodeAt(pt); 41 42 #region 移动节点 43 if (targetNode != null 44 && targetNode.Parent != currentNode 45 && targetNode != currentNode) 46 { 47 TreeNode tempNode = currentNode; 48 if (pt.X < 30 && targetNode.Parent == currentNode.Parent) 49 { 50 #region 同级移动顺序 51 if (targetNode.Parent != null) 52 { 53 currentNode.Remove(); 54 targetNode.Parent.Nodes.Insert(targetNode.Index, tempNode); 55 } 56 #endregion 57 } 58 else 59 { 60 #region 同级或跨节点移动 61 //if (currentNode.Parent != null && currentNode.Parent.Nodes.Count == 1) 62 //{ 63 // currentNode.Parent.ImageIndex = 2; 64 // currentNode.Parent.SelectedImageIndex = currentNode.Parent.ImageIndex; 65 //} 66 currentNode.Remove(); 67 targetNode.Nodes.Add(tempNode); 68 //targetNode.ImageIndex = 0; 69 //targetNode.SelectedImageIndex = targetNode.ImageIndex; 70 #endregion 71 } 72 } 73 #endregion 74 75 #region 拖至根节点 76 if (targetNode == null) 77 { 78 TreeNode tempNode = currentNode; 79 //if (currentNode.Parent != null 80 // && currentNode.Parent.Nodes.Count == 1) 81 //{ 82 // currentNode.Parent.ImageIndex = 2; 83 // currentNode.Parent.SelectedImageIndex = currentNode.Parent.ImageIndex; 84 //} 85 currentNode.Remove(); 86 treeViewMenu.Nodes.Add(tempNode); 87 } 88 #endregion 89 } 90 catch (Exception ex) 91 { 92 MessageBox.Show("Error:" + ex.ToString(), "错误提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); 93 } 94 }