TreeView 节点的图像索引在选择时发生变化
当我尝试在树视图中使用图像列表时,单击树节点时图像索引会发生变化。我不知道为什么会发生这种情况。谁能帮助我吗?
提前致谢
When I tried using the imagelist in treeview, the image index changes when treenode is clicked. I have no idea why it is happening. Can anyone help me?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在树节点上设置 ImageIndex 和 SelectedImageIndex。
You need to set both the ImageIndex and the SelectedImageIndex on the tree node.
“SelectedImageIndex”的目的是允许在选择时显示与“ImageIndex”为特定节点设置的图像不同的图像。为了保持这两个一致,有必要将它们设置为相同的值。这可以在设计时完成,也可以根据您的需要以编程方式完成。
例如,如果图像永远不会更改,那么就像在将新节点添加到 TreeView 时同时设置它们一样简单:
但是,如果您在初始创建后出于任何原因更改了 ImageIndex 值(例如响应某些一种用户操作),那么您还必须更改 SelectedImageIndex。否则,它们就会变得不一致。
(请注意,在“AfterSelect”事件的事件处理程序中将它们设置为相同是不够的。必须在代码中 ImageIndex 发生更改的任何位置完成此操作。)
'SelectedImageIndex's intent is to allow displaying a different image upon selection than what is set by the 'ImageIndex' for a particular node. To keep these two consistent it is necessary to set them to the same value. This can be done at design time or programmatically depending on your needs.
For example, if the images never change then it is as simple as setting them concurrently when a new node is added to the TreeView:
However, if you do change the ImageIndex value for any reason after its initial creation (such as a response to some kind of user action), then you must also change the SelectedImageIndex as well. Otherwise, they will become inconsistent.
(Note it is not enough to set them to be the same in the event handler of the 'AfterSelect' event. It must be done anywhere in your code where ImageIndex changes.)
您可以直接在构造函数中执行此操作:
you can directly do it in the constructor :
只需添加此行:
Node.SelectedIndex:=Node.ImageIndex;
Just Add this line:
Node.SelectedIndex:=Node.ImageIndex;