Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / XML

Multi-select Treeview control v2.0

Rate me:
Please Sign up or sign in to vote.
4.64/5 (16 votes)
15 Aug 2004CPOL 230.5K   6.2K   48   52
Multi-select Treeview control v2.0

Screenshots

Image 1

Image 2

Image 3

Image 4

Image 5

Image 6

Image 7

Image 8

Image 9

Introduction

This is a multi select TreeView Control for the .NET Framework. It is possible to select TreeNodes in multiple ways with or without constraints.

Details

The MWTreeView has a few ways of mouse-selecting the TreeNodes by painting anything from a rubberband to very graphical representations. There is even the possibility, using GDI+, to customize the mouse-selection. It is also possible to have TreeNodes of various colors within the same MWTreeView with multi-selection still working. The MWTreeView has a massive amount of configurable settings so that anyone should be able to tailor it to their needs.

Note that when selecting TreeNodes in code, the SelectNode (DeselectNode also exists) method should be used. Note that when changing the colors of a TreeNode, the ChangeColors method should be used. Do not just change the colors of a TreeNode.

Note that in order to iterate through the selected TreeNodes, the following method has to be used (selected TreeNodes are stored in a Hashtable):

C#
foreach(MWTreeNodeWrapper mwtnw in mwtvMWTreeView.SelNodes.Values)
{
   MessageBox.Show("The " + mwtnw.Node.Text + 
    " TreeNode is selected.");
}

Contacting Me

I can be reached through the forum here at CodeProject.

License

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


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSorting selected nodes Pin
Abrakadabraaaaa25-Jul-22 23:00
Abrakadabraaaaa25-Jul-22 23:00 
QuestionOption to select children when parent is selected Pin
revitarkitek12-Feb-20 7:28
revitarkitek12-Feb-20 7:28 
QuestionHow to access checked items? Pin
Alexander Pikus3-Nov-08 19:09
Alexander Pikus3-Nov-08 19:09 
GeneralDraga and Drop Behavior Pin
Dewayne Dodd5-Mar-08 10:45
Dewayne Dodd5-Mar-08 10:45 
GeneralRe: Draga and Drop Behavior Pin
vinutha kempanna5-Mar-08 10:57
vinutha kempanna5-Mar-08 10:57 
GeneralRe: Draga and Drop Behavior Pin
Mikael Wiberg5-Mar-08 19:03
Mikael Wiberg5-Mar-08 19:03 
QuestionGreat code, license question? Pin
thatrickguy29-Nov-07 5:53
thatrickguy29-Nov-07 5:53 
AnswerRe: Great code, license question? Pin
Mikael Wiberg29-Nov-07 6:42
Mikael Wiberg29-Nov-07 6:42 
GeneralOdd behaviour on deselect [modified] Pin
benjymous19-Sep-07 22:09
benjymous19-Sep-07 22:09 
GeneralNodes.Clear Doesn't Clear SelNodes Pin
Christopher Theunissen20-Apr-07 1:16
Christopher Theunissen20-Apr-07 1:16 
GeneralRe: Nodes.Clear Doesn't Clear SelNodes Pin
Mikael Wiberg20-Apr-07 6:07
Mikael Wiberg20-Apr-07 6:07 
QuestionSearch as you type doesn't work on the SameLevelMultiBranch Pin
DotNetWise2-Apr-07 0:58
DotNetWise2-Apr-07 0:58 
AnswerRe: Search as you type doesn't work on the SameLevelMultiBranch [modified] Pin
Mikael Wiberg9-Apr-07 0:27
Mikael Wiberg9-Apr-07 0:27 
QuestionCheckboxes Pin
DotNetWise22-Dec-06 12:59
DotNetWise22-Dec-06 12:59 
AnswerRe: Checkboxes Pin
DotNetWise22-Dec-06 14:17
DotNetWise22-Dec-06 14:17 
GeneralRe: Checkboxes Pin
DotNetWise22-Dec-06 14:53
DotNetWise22-Dec-06 14:53 
GeneralRe: Checkboxes Pin
Mikael Wiberg22-Dec-06 17:46
Mikael Wiberg22-Dec-06 17:46 
GeneralComercial Version Pin
FooDogCom16-Dec-06 13:44
FooDogCom16-Dec-06 13:44 
GeneralRe: Comercial Version Pin
Mikael Wiberg16-Dec-06 20:06
Mikael Wiberg16-Dec-06 20:06 
GeneralProblem with the highlighting/unhighlighting Pin
jebrew20-Sep-06 6:08
jebrew20-Sep-06 6:08 
GeneralLatest Version with DataBinding Pin
Jacob Shepherd29-Nov-05 12:46
Jacob Shepherd29-Nov-05 12:46 
GeneralDeep Logic bug about selecting node(s) Pin
Le_MuLoT13-Sep-05 4:17
Le_MuLoT13-Sep-05 4:17 
AnswerRe: Deep Logic bug about selecting node(s) Pin
Mikael Wiberg14-Sep-05 2:04
Mikael Wiberg14-Sep-05 2:04 
Thanks for the nice words.
I think I have written more code to 'help' the MS TreeView than the whole MS TreeView required... Roll eyes | :rolleyes:

Note that there is no bug when selecting TreeNodes when programming for it properly in current versions. Read on and I will explain why.

The code has changed quite a bit for the latest versions. This is what the static Deselect method looks like now:
public static void Deselect(MWTreeNodeWrapper mwtnw)<br />
{<br />
    mwtnw.Node.ImageIndex = mwtnw.ImageIndex;<br />
    mwtnw.Node.SelectedImageIndex = mwtnw.SelectedImageIndex;<br />
<br />
    if( mwtnw != null &&<br />
        mwtnw.Node != null &&<br />
        (mwtnw.Node.TreeView == null ||<br />
        mwtnw.Node.TreeView.Enabled))<br />
    {<br />
        mwtnw.Node.BackColor = mwtnw.BackColor;<br />
        mwtnw.Node.ForeColor = mwtnw.ForeColor;<br />
    }<br />
}

So no exceptions should be thrown.

It is really up to the programmer using the MWTreeView to remove TreeNodes from the SelNodes collection if they are removed from the MWTreeView.
I.e. if a ContextMenu has a 'Delete Node' item then the code behind this item should both remove the TreeNode from the SelNodes collection and from its parent (whether that is a TreeNode or the TreeView itself).

It is really a shame that you cannot instantiate TreeNodeCollections etc. Hope this changes sometime in the future.

What you write about checking for orphan TreeNodes (orphan, parent, child - what is this, some family reunion or something? Wink | ;) ) would not be necessary if the code where TreeNodes are removed, cleared or whatever also deals with the SelNodes collection.

If we could override the TreeNodeCollections class etc the whole job of the MWTreeView would have been so much easier.
But part of the challenge is to be able to do it to the current MS TreeView Wink | ;)


Considering all your TreeView-related questions, are you creating your own TreeView, inheriting from the MS TreeView or using the MWTreeView (curious)?
GeneralRe: Deep Logic bug about selecting node(s) Pin
Le_MuLoT14-Sep-05 3:13
Le_MuLoT14-Sep-05 3:13 
GeneralRe: Deep Logic bug about selecting node(s) Pin
Mikael Wiberg14-Sep-05 3:51
Mikael Wiberg14-Sep-05 3:51 

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.