在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;
}
}