鼠标悬停时如何获取Treeview节点 [英] How to get the treeview node when mouse hover

查看:61
本文介绍了鼠标悬停时如何获取Treeview节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的windowform应用程序具有以下语句:

1.有一个名为TreeView1的TreeView对象和一个事件处理程序
TreeView1_NodeMouseHover.

2.在事件处理程序中,我编写如下代码:

My windowform application has following statements :

1. There is one TreeView object named TreeView1, and one event handler
TreeView1_NodeMouseHover.

2. In the event handler I write code as follows :

Point Cp = Cursor.Position;
 Point Cp_client = TreeView1.PointToClient(Cp);
 Control HitNode = new Control();
 HitNode = TreeView1.GetChildAtPoint(Cp_client);
 Label1.Text = HitNode.Text;
 Label.Location = new Point(HitNode.Bounds.X, HitNode.Bounds.Y);



我想精确地在鼠标悬停在其上的节点上显示label1,但是,尽管当我将鼠标悬停在专用节点上时会调用事件处理程序,但我始终会得到null HitNode.

任何人都可以显示出为什么我总是得到空HidNode的根本原因吗?
另外,是否有任何与treeview.getchildatpoint方法相关的示例代码?

我也想移动节点,是否有任何示例代码可供参考?谢谢.



I would like to show label1 exactly on the node which mouse hovers on it, however, I always get null HitNode though the event handler is invoked when I move mouse to hover the dedicated node.

Can anybody show the root cause why I always get the null HidNode ?
Plus, is there any sample code related to the treeview.getchildatpoint method ?

I also would like to move the node, is there any sample code for reference ? Thanks.

推荐答案

简单:请勿使用MouseHover.

改用NodeMouseHover-TreeNodeMouseHoverEventArgs的Node属性为您提供节点.



为了扩展这个答案-代码不起作用的原因(无论如何调整,它永远都不会起作用)非常简单:节点不是控件.

虽然TreeView基于控件(Object--MarshalByRefObject< -Component< -Control< -TreeView),但是它绘制其节点本身,而不是具有节点控件类型. TreeNode(Object< -MarshalByRefObject< -TreeNode)不是可显示的项目,因此它将永远无法工作-它没有位置或任何其他与显示相关的属性!

除此之外,您为什么要尝试将标签移动到节点的顶部?取决于整个负载因素(zOrder等),您尝试使用标签做的任何事情都不太可能起作用,因此也许有更好的方法来实现目标...

[/edit block]
Simple: Don''t use MouseHover.

Use NodeMouseHover instead - the Node property of the TreeNodeMouseHoverEventArgs gives you the node.

[edit block]

To expand on this answer - the reason your code doesn''t work (and never will, no matter how you tweak it) is quite simple: a node is not a control.

While a TreeView is based on a control (Object<-MarshalByRefObject<-Component<-Control<-TreeView) it draws it''s nodes itself, rather than having a node control type. A TreeNode (Object<-MarshalByRefObject<-TreeNode) is not a displayable item, so it will never work - it doesn''t have a Location, or any other display related properties!

And to add to this, why are you trying to move a label over the top of a node? Whatever you are trying to do with the label is unlikely to work, depending on a whole load of factors (zOrder, etc), so perhaps there is a better way to achieve your ends...

[/edit block]


这篇关于鼠标悬停时如何获取Treeview节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆