Click here to Skip to main content
Licence 
First Posted 13 Sep 2007
Views 12,559
Downloads 310
Bookmarked 11 times

TreeView with multi selection feature

By | 13 Sep 2007 | Article
TreeView with multi selection feature
Screenshot - TreeViewMultiSelectSample.jpg

Introduction

This control extends the Treeview of .NET in a very simply but effective manner. There are just a few modifications that provide multiselection, the same way, the Visual Studio 2005 does in its Project-Explorer.

Using the code

Copy the source treeview.cs into your project and use it instead of the original treeview that comes with .NET. All behaviour of the original control remains.

You can use the shift and control keys to select multiple items by keyboard and mouse.

New Property: MultiSelect

There is a new property MultiSelect, that you can use to switch multiselection on and off.

New Property: SelectedNodes

There is another property SelectedNodes, that lets you retrieve or set the multiselection. The original property SelectedNode is working as well, but it hold only the last selected node.

Full Row Selection

I also implemented a full-row-selection like in Visual Studio 2005. If you don't like that, remove the out crossed code.

protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);

    TreeViewHitTestInfo treeViewHitTestInfo = null;
    switch (e.Button)
    {
        case MouseButtons.Right:

            treeViewHitTestInfo = this.HitTest(e.Location);
            if (treeViewHitTestInfo.Location == TreeViewHitTestLocations.Label ||
                treeViewHitTestInfo.Location == TreeViewHitTestLocations.RightOfLabel)
            {
                if (!this.selectedNodes.Contains(treeViewHitTestInfo.Node))
                {
                    this.ClearPreviousSelection();
                    this.SelectedNode = treeViewHitTestInfo.Node;
                }
            }
            break;

        case MouseButtons.Left:

            treeViewHitTestInfo = this.HitTest(e.Location);
            if (treeViewHitTestInfo.Location == TreeViewHitTestLocations.RightOfLabel)
            {
                this.SelectedNode = treeViewHitTestInfo.Node;
            }
            break;
    }
}

ContextMenu Click

As you may see in the code above, the right-click does a selection, if an unselected node is hit. This is very important, because the original treeview resets the selection after the click and so the context-menu may show wrong items.

Points of Interest

I've tested different multiselection tree controls and every time I noticed, they was made of a lot of code. Often the functionality was overloaded with nice looking things but the keyfunctionality did not work with every users input. Now I did it myself and I realized: Keep it simple, as much as you can. Multiselection can easily get very complex.

History

I would be grateful for any suggestions.

Greatings,

Jean Allisat

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Allisat Software GmbH

Web Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice article PinmemberNicolasPasc22:44 18 Apr '12  
GeneralExcellent PinmemberXavier Lelievre4:43 22 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Sep 2007
Article Copyright 2007 by Allisat Software GmbH
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid