Results 1 to 7 of 7

Thread: How to set icons for the windows forms treeview control ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

    How to set icons for the windows forms treeview control ?

    I use imageList to store icons and I assign imageList to treeView, I use treeView1_DrawNode to change icon for treeView but this function does not run. See my photo and code below.
    Code:
    namespace TreeView
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    treeView1.ImageList = imageList1;
    this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode);
    dt = CreateTable(10);
    CreateTreeView(dt, treeView1, true);
    }
    DataTable dt = new DataTable();
    ... //My code
    private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
    if (e.Node.IsExpanded || e.Node.Nodes.Count < 1)
    e.Node.ImageIndex = 0;
    else
    e.Node.ImageIndex = 1;
    }
    }
    Name:  treeView02.jpg
Views: 2316
Size:  27.1 KB
    Why is the treeView1_DrawNode(...) function not running ? How to change my treeView icons insted of +,- like a windows explorer treeview in win forms ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to set icons for the windows forms treeview control ?

    You don't handle the DrawNode event unless you have set the DrawMode property to a value other than Normal and you don't do that unless you actually want to write your own code to draw the nodes using GDI+. If all you want to do is change the image for a node when it is expanded or collapsed then that's what you do. The BeforeExpand and BeforeCollapse events are raised before a node is expanded or collapsed and the AfterExpand and AfterCollapse events occur afterwards. In practice, it probably wouldn't matter which pair you choose to handle but, given that the Before pair are specifically intended to allow you to cancel the operation, best practice would be to handle the After pair. Your code should look something like this:
    vb.net Code:
    1. private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
    2. {
    3.     e.Node.ImageIndex = 0;
    4. }
    5.  
    6. private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
    7. {
    8.     e.Node.ImageIndex = 1;
    9. }
    It's not completely clear what image those two indexes refer to so I'm just guessing there. I would suggest setting a key for each image and then using ImageKey instead, as the text of the key will be more informative than a number.

    Also, you shouldn't really be setting the ImageList property or registering event handlers in code. They should both be done in the designer and, specifcally, in the Properties window. Once you have added the ImageList instance to the form, you can set the ImageList property by selecting the instance from the drop-down. You can then click the Events button and then generate event handlers by double-clicking the appropriate event. The corresponding code is then auto-generated in the designer code file.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

    Re: How to set icons for the windows forms treeview control ?

    According to your guide I have designed but I want to change icon of 2 node: the first node and the last node how to determine the first node and the last nodes to change 2 icons of these 2 nodes ? You see my attached photo
    Sample photo
    Name:  treeView05.jpg
Views: 2207
Size:  41.9 KB

    My photos are designed to look like a sample image
    Name:  treeView06.jpg
Views: 2468
Size:  30.3 KB

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to set icons for the windows forms treeview control ?

    If you want more than two icons displayed in your TreeView then add more than two icons to your ImageList. Just set the ImageIndex or ImageKey of each node to the value that corresponds to the icon you want displayed for that node. As I said, I would suggest using a key because that is much more descriptive than an index. You could use "Home", "Bin", "OpenFolder" and "ClosedFolder" as the keys and then its obvious what your intention is when you set the ImageKey of a node. You could even declare your own Enum so as not to be using magic strings.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

    Re: How to set icons for the windows forms treeview control ?

    I have added 10 icons to ImageList but in the treeView1_AfterExpand and treeView1_AfterCollapse functions, how do I determine the first and last node to assign the icon index to these 2 nodes ?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to set icons for the windows forms treeview control ?

    That doesn't seem to make sense. The point of those events is to detect when a node is expanded or collapsed and give you access to that node so that you can make a change to that node, e.g. change the icon from a closed folder to an open folder and back again, which is clearly visible in the images you posted. Why would you want to change the first or last node when, for instance, you just expanded the third node?

    That said, you can access any node in the tree by index. Are you really saying that you don't know how to determine the first and last index in a collection?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    517

    Re: How to set icons for the windows forms treeview control ?

    Currently I have not found a place to change the first node and last node icons, I am searching and reading documents on the internet to change the first and last node icons, if someone knows I ask them to point back faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width