private Point pi;//定义一个坐标变量 //TreeRule为TreeView的Name private void TreeRule_MouseDown(object sender, MouseEventArgs e)//当鼠标指针位于控件上并按下鼠标键时发生 { pi = new Point(e.X, e.Y);//记录当前位置 } private void TreeRule_DoubleClick(object sender, EventArgs e) { TreeNode node = this.TreeRule.GetNodeAt(pi);//检索位于指定点的树节点 if (pi.X >= node.Bounds.Left && pi.X <= node.Bounds.Right)//当鼠标双击的位置在某个结点上时 { editMenuTree.LabelEdit = true;//开启树结点的标签文本编辑状态 if (!TreeRule.SelectedNode.IsEditing) { TreeRule.SelectedNode.BeginEdit();//使该结点进入编辑状态 } } } private void TreeRule_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)//编辑状态结束时触发 { if(e.Label != null) { if (e.Label.Trim().Length > 0)//输入的值不为空 { string sql = string.Format("update region set menuname = '{0}' where id = {1}",e.Label.Trim(),Convert.ToInt32(e.Node.Name));//进行更新操作 if (db.ExecuteSql(sql) != 1)//执行sql语句失败 { e.CancelEdit = true;//取消当前编辑,恢复到最初状态 }
TreeNodeName = e.Label.Trim();
TreeRule.SelectedNode.Text = TreeNodeName;
} else { e.CancelEdit = true; } } }