开发者

Image index of TreeView node changes upon selection

When I tried using the imagelist in treeview, the image index changes when treenode 开发者_Python百科is clicked. I have no idea why it is happening. Can anyone help me?

Thanks in advance


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


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


you can directly do it in the constructor :

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


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


Just Add this line:

Node.SelectedIndex:=Node.ImageIndex;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜