if I click on a node and it is a file, the image automatically changes to the folder image.
You can see this in the picture.

There is no MouseClick-Event - even a MouseDoubleClick-Event.
Code-Snippets:
Add the Icons to the ImageList:
private void LoadImageList()
{
imageList = new ImageList();
imageList.ImageSize = new System.Drawing.Size(16, 16);
imageList.Images.Add("folder", new System.Drawing.Icon(Application.StartupPath + "\\res\\" + "folder2.ico"));
imageList.Images.Add("file", new System.Drawing.Icon(Application.StartupPath + "\\res\\" + "file2.ico"));
tVDirectories.ImageList = imageList;
}
Update the TreeView:
private void UpdateTreeView(string pFtpPath, TreeNode pCurrentTreeNode)
{
[...]
TreeNode hostNode = tVDirectories.Nodes.Find("ftpServer", false)[0];
hostNode.Text = _ftpServerFullPath;
TreeNode childNode;
List<string> ftpDirectories = GetFtpCurrentDirectoryList(pFtpPath);
foreach (string item in ftpDirectories)
{
childNode = new TreeNode(item);
childNode.Name = item;
if (item.Contains('.'))
{
childNode.ImageIndex = 1; //.ImageKey = "file";
}
else
{
childNode.ImageIndex = 0; //.ImageKey = "folder";
}
hostNode.Nodes.Add(childNode);
}
}
Actually the problem comes from the fact that the ImageKey sets the image only when your node is not selected. You should use ImageIndex instead: a good example can be found here.
Quoting MSDN related page:
The key of the default image shown for each node TreeView control when the node is in an unselected state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With