TreeView 节点的图像索引在选择时发生变化

发布于 2024-08-09 03:10:30 字数 75 浏览 9 评论 0原文

当我尝试在树视图中使用图像列表时,单击树节点时图像索引会发生变化。我不知道为什么会发生这种情况。谁能帮助我吗?

提前致谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天气好吗我好吗 2024-08-16 03:10:30

您需要在树节点上设置 ImageIndex 和 SelectedImageIndex。

You need to set both the ImageIndex and the SelectedImageIndex on the tree node.

故人爱我别走 2024-08-16 03:10:30

“SelectedImageIndex”的目的是允许在选择时显示与“ImageIndex”为特定节点设置的图像不同的图像。为了保持这两个一致,有必要将它们设置为相同的值。这可以在设计时完成,也可以根据您的需要以编程方式完成。

例如,如果图像永远不会更改,那么就像在将新节点添加到 TreeView 时同时设置它们一样简单:

int myCurrentImageIndex = 0;
TreeNode node = myTreeView.Nodes.Add("new node!");
node.ImageIndex = node.SelectedImageIndex = myCurrentImageIndex;

但是,如果您在初始创建后出于任何原因更改了 ImageIndex 值(例如响应某些一种用户操作),那么您还必须更改 SelectedImageIndex。否则,它们就会变得不一致。

int myNewImageIndex = 1;
node.ImageIndex = node.SelectedImageIndex = myNewImageIndex;

(请注意,在“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:

int myCurrentImageIndex = 0;
TreeNode node = myTreeView.Nodes.Add("new node!");
node.ImageIndex = node.SelectedImageIndex = myCurrentImageIndex;

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.

int myNewImageIndex = 1;
node.ImageIndex = node.SelectedImageIndex = myNewImageIndex;

(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.)

时光清浅 2024-08-16 03:10:30

您可以直接在构造函数中执行此操作:

TreeNode node = new TreeNode("My treenode", 1, 1);

you can directly do it in the constructor :

TreeNode node = new TreeNode("My treenode", 1, 1);
驱逐舰岛风号 2024-08-16 03:10:30

只需添加此行:

Node.SelectedIndex:=Node.ImageIndex;

Just Add this line:

Node.SelectedIndex:=Node.ImageIndex;

风渺 2024-08-16 03:10:30
TreeNode tn = new TreeNode();
tn.Text = "NewRecord";
tn.ImageIndex = 1;

treeView.SelectedNode.Nodes.Add(tn);
treeView.SelectedNode = tn;
treeView.SelectedNode.SelectedImageIndex = tn.ImageIndex; // <--- Problem solved
tn.BeginEdit();
TreeNode tn = new TreeNode();
tn.Text = "NewRecord";
tn.ImageIndex = 1;

treeView.SelectedNode.Nodes.Add(tn);
treeView.SelectedNode = tn;
treeView.SelectedNode.SelectedImageIndex = tn.ImageIndex; // <--- Problem solved
tn.BeginEdit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文