Click here to Skip to main content
13,350,047 members (46,881 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 4 Apr 2008

multiselect treenode

, 4 Apr 2008
Rate this:
Please Sign up or sign in to vote.
Easy way to implement multiselect in treeviews without using custom controls

Selecting multiple nodes in treevieiw

Source code of my example can be found here:

Download - 37.21 KB

If you're working with treeviews wishing that you had a multiple node select feature, this code is just meant for you. Most of the articles that i read in the internet either suggested me to use some custom tree control or the suggested codes seemed real complex to me. Hence i wrote the code myself so that anyone willing to use this feature could do it in their existing tree views without having to retort to custom controls.


The idea behind the code is simple, I've globally maintained a List of treenodes. When the user clicks a node, any modifier key like Ctrl is checked if pressed. If no modifier key is found to be pressed, the list is cleared and the node is added to the list. If there is a Ctrl key is pressed, then we add the node to the list, In the end, i've put up a function which paints the tree such that only those node present in the list are highlighted.

Using the code

The function below, treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e) is called before any node in the treeview is selected. Another important line of code is, e.Cancel = true;. Without this code, when we deselect the node, the windows function paints it once again. As a result the treeview nodes doesnt change colors as expected. Canceling this event argument, prevents the windows from repainting the node as selected one.

private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
    if (ModifierKeys == Keys.Control)
        if (trSelectedNodes.Contains(e.Node))
        else trSelectedNodes.Add(e.Node);
    e.Cancel = true;


This can also be done, by writing the same thing on the NodeMouseClick() function :) I shall post the code soon.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ayush Shrestha
Student Georgia State University
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalgoood Pin
wadeliu29-Oct-08 0:09
memberwadeliu29-Oct-08 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 4 Apr 2008
Article Copyright 2008 by Ayush Shrestha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid