treeview中鼠标右键节点的问题
我想treeview中鼠标右键节点时,就可以选择这个节点,有什么办法?
怎么这样不行?
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button==MouseButtons.Right)
{
e.Node.Checked = true;
}
}
------解决方案--------------------你可以通过GetNodeAt方法来得到鼠标下的这个节点:
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
TreeNode node = this.treeView1.GetNodeAt(e.Location);
if (node != null)
{
this.treeView1.SelectedNode = node;
}
}
------解决方案--------------------//顶楼上
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
((TreeView)sender).SelectedNode = ((TreeView)sender).GetNodeAt(e.Location);;
}
}
------解决方案--------------------有2中方法:
1)GetNodeAt
2)Hittest
------解决方案--------------------对,ls说的两种办法都可以解决,给你另一个例子:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
treeView1.SelectedNode = e.Node;
}
}