在TreeView节点上点击右键触发选中该节点的方法

来源:互联网 发布:数据分析师培训多少钱 编辑:程序博客网 时间:2024/05/17 02:04

  在WinForm开发中在TreeView控件上点击鼠标右键是不能引起SelectNode属性变化的,这会造成右键菜单显示不正确的问题。
解决办法是在treeView控件的mousedown事件中加入如下代码 :

在事件中增加委托
        this.treeViewActive.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);

委托操作
private void treeView_MouseDown(object sender, MouseEventArgs e)        {            TreeView tv = sender as TreeView;            if (tv == null)            {                return;            }            if (e.Button == MouseButtons.Right)            {                TreeNode selectedNode = tv.GetNodeAt(e.Location);                tv.SelectedNode = selectedNode;            }        }

 
原创粉丝点击