Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image index of TreeView node changes upon selection

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

like image 597
Sudhir Avatar asked Nov 01 '09 06:11

Sudhir


People also ask

How do I know if TreeView node is selected?

SelectedNode; if ( tn == null ) Console. WriteLine("No tree node selected."); else Console. WriteLine("Selected tree node {0}.", tn.Name ); You can compare the returned TreeNode reference to the TreeNode you're looking for, and so check if it's currently selected.

How do you edit TreeView?

TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView class to true. The default value of the property is false. You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node itself.

What is TreeView in C#?

TreeView control in C# is used to display the items in hierarchical form. Each item in TreeView control is called a node. A node can have sub-nodes too; and each sub-node has it's own nodes, and so on. All the nodes in the TreeView control are displayed in an hierarchical form, for better readability and control.


3 Answers

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

like image 87
Matt Breckon Avatar answered Sep 18 '22 19:09

Matt Breckon


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

like image 23
Ray Avatar answered Sep 20 '22 19:09

Ray


you can directly do it in the constructor :

TreeNode node = new TreeNode("My treenode", 1, 1);
like image 21
Buenjy Avatar answered Sep 20 '22 19:09

Buenjy