Search code examples
c#treeviewtreeviewitem

CSharp - Treeview ImageKey didnt work


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.

enter image description here
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);
     }
}

Solution

  • The solution is the following:

    @kamino said there is a good example and there I found the following: myTreeView.SelectedImageIndex = 0;

    And that's it:

    childNode.ImageIndex = 1; // ImageKey = "file";
    childNode.SelectedImageIndex = 1;