Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / XML

Multiselect Treenode

Rate me:
Please Sign up or sign in to vote.
3.80/5 (5 votes)
4 Apr 2008CPOL1 min read 35.8K   540   14   5
Easy way to implement multiselect in treeviews without using custom controls

Selecting Multiple Nodes in treevieiw

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 on 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 resort to custom controls.

Background

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 nodes 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 doesn't change colors as expected. Canceling this event argument, prevents the windows from repainting the node as selected one.

C#
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if (ModifierKeys == Keys.Control)
    {
        if (trSelectedNodes.Contains(e.Node))
            trSelectedNodes.Remove(e.Node);
        else trSelectedNodes.Add(e.Node);
    }
    else
    {
        trSelectedNodes.Clear();
        trSelectedNodes.Add(e.Node);
    }
    trPaintSelected();
    showSelectedInBox();
    e.Cancel = true;
}

Alternatives

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

History

  • 4th April, 2008: Initial version

License

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



Comments and Discussions

 
QuestionI want to edit the label. Pin
hisuzuki_197507315-Oct-09 0:36
hisuzuki_197507315-Oct-09 0:36 
GeneralGood, but lot of loops... Pin
gpggpg13-Dec-08 3:37
gpggpg13-Dec-08 3:37 
Generalgoood Pin
wadeblack28-Oct-08 23:09
wadeblack28-Oct-08 23:09 
GeneralGood one Pin
ring_017-Jun-08 23:43
ring_017-Jun-08 23:43 
You saved my day. Thanks a lot

Man having 1 bit brain with parity error

GeneralWould be great Pin
Jefis4-Apr-08 2:54
Jefis4-Apr-08 2:54 

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.