Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / XML

Multi-Select TreeView Control in C#

Rate me:
Please Sign up or sign in to vote.
4.88/5 (20 votes)
15 Jun 2003CPOL5 min read 339.8K   3.7K   71   81
A TreeView control that implements multiple selection in different ways

Sample Image - mwcontrols03.gif

Introduction

This latest version includes a multi-select TreeView control which lets you choose a few different ways of selecting TreeNodes.

There is a test project set up (MWControlsTest) for testing all of the MWControls.

MWControls contains:

  • MWTreeView: A Multi Select TreeView Control (MultiSelect, Multi-Select)
  • MWScrollLabel: A Label Control that scrolls the text back and forth if it does not fit when the mouse is hovering over it.
  • The MWScrollLabel Control also supports displaying the text in different directions: Normal, UpsideDown, Left and Right.
  • MWLabel: A Label control that uses StringFormat.GenericTypographic, meaning that the text doesn't require a large blank area next to it.
  • The MWLabel control also supports displaying the text in different directions: Normal, UpsideDown, Left and Right.

Coding Technologies Used

MWTreeView

  • Multi-select for TreeNodes.
  • Overridden CancelEventArgs so that AfterXXX and BeforeXXX event handlers can be set up allowing the programmer to cancel setting properties.

MWScrollLabel

  • A timer is used for the scrolling.

MWLabel and MWScrollLabel

  • GDI+ is used to draw everything (text and image)
  • The text is rendered properly as disabled when the Enabled property is set to false
  • A graphical Design Time Editor inheriting from UITypeEditor is used for the TextDir property

All

  • Inheritance including overriding event handlers and programming around the fact that some methods, etc. in the parent control are hidden

Shortcomings

  • MWLabel and MWScrollLabel: The UseMnemonic property is not used. (I don't intend to fix this myself. If someone has a solution, please let me know.)
  • MWTreeView: When HotTracking is turned on, the colors are not correct. (I will probably not fix this myself. If someone has a solution, please let me know.)

Version History and More about the Controls

Version 1.0.3.0

MWTreeView:

  • The MWTreeView control supports proper multi-select functionality. When I say proper, I mean better than any other free one I have seen out there (C# or other).
  • MultiSelect is a property of the MWTreeView control and can be set to one of the following values:
    • NoMulti: Multi-selection is turned off. All other features are still available though, so it still works better than a normal TreeView Control.
    • Multi: Free unrestricted multi-selection - any TreeNodes can be selected.
    • MultiSameBranchAndLevel: Multiple TreeNodes can only be selected if they are in the same branch and on the same level.
    • MultiSameBranch: Multiple TreeNodes can only be selected if they are in the same branch.
    • MultiSameLevel: Multiple TreeNodes can only be selected if they are on the same level.
  • Several keys now do things to the MWTreeView: Ctrl+A selects all TreeNodes (according to the restrictions imposed by the MultiSelect property), etc, etc.
  • TreeNodes can be selected using the mouse in combination with the control, shift and alt keys.
  • If the MultiSelect property is set to a value other than NoMulti, no TreeNode has to be selected at all. and once one is selected, it can be deselected (control and mouse click).
  • The FullRowSelect property of the original TreeView control didn't seem to work (please correct me if I'm wrong). It has now been implemented.
  • There is a massive amount of code (more than 3400 lines in the MWTreeView class alone), so I guess this is an example (hopefully a good one) of how to subclass a control and add a lot of functionality.

Version 1.0.2.0

MWControlsTest

  • The MWControlsTest project has been added, making it really easy to test all the features of the MWControls.
  • Any new controls added to MWControls can appear on a new TabPage.

Version 1.0.1.0

MWScrollLabel

  • The MWScrollLabel is a subclassed Label control that can be set to scroll the text back and forth if the whole text does not fit. The way I am using this is by starting the scrolling when the mouse is hovering over the control.
  • There are quite a few options that can be set to control the scrolling.
  • Note that the MWScollLabel control is only displaying its text on one line. This is intentional and coupled with its scrolling capabilities is really the whole reason for implementing this control.

Version 1.0.0.0

MWLabel

  • The StringFormat used by the text can be set to one of two options: GenericDefault and GenericTypographic.
    • GenericDefault: This is the StringFormat used by the original Label control.
    • GenericTypographic: This StringFormat is much more precise when it comes to measuring the string.
  • Using the GenericTypographic option (default) means that if the text is right-aligned, it will indeed properly align to the right even for very long words with no white-space.
  • The text direction can be set:
    • Normal: Normal horizontal text
    • UpsideDown: Horizontal text displayed upside down
    • Left: Vertical text that can be read by tilting your head to the left
    • Right: Vertical text that can be read by tilting your head to the right
  • There is an option of displaying an image (the normal Image property or the image from the ImageIndex property) over the text or like the normal Label control under the text.
  • The image (the normal Image property or the image from the ImageIndex property), if one is used, is displayed very close to the applicable edge - not some strange arbitrary distance from the edge.

Disclaimer

Mikael Wiberg 2003

Feel free to use this code as you wish, as long as you do not take credit for it yourself. If it is used in commercial projects or applications, please mention my name. Feel free to donate any amount of money if this code makes you happy. ;) Use this code at your own risk. If your machine blows up while using it - don't blame me.

History

  • 16th June, 2003: Update

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.

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

 
GeneralRe: Problem setting the Checked Property of a TreeNode Pin
Mikael Wiberg30-Nov-06 7:05
Mikael Wiberg30-Nov-06 7:05 
QuestionCopy the nodes in slection order Pin
celsoalejo5-Sep-06 11:04
celsoalejo5-Sep-06 11:04 
AnswerRe: Copy the nodes in slection order Pin
Mikael Wiberg5-Sep-06 18:54
Mikael Wiberg5-Sep-06 18:54 
Generalwhen tree loses focus, there is no highlighting :-( Pin
syzygyfire15-Aug-06 0:03
syzygyfire15-Aug-06 0:03 
GeneralRe: when tree loses focus, there is no highlighting :-( Pin
syzygyfire15-Aug-06 0:15
syzygyfire15-Aug-06 0:15 
QuestionSelNodesChanged - Bug? Pin
George Srb10-Aug-06 20:12
George Srb10-Aug-06 20:12 
AnswerRe: SelNodesChanged - Bug? Pin
qwer24135412343-May-11 22:32
qwer24135412343-May-11 22:32 
QuestionImageIndex is not working ? - Bug Pin
Dmitry Dyachkov4-Aug-06 0:50
Dmitry Dyachkov4-Aug-06 0:50 
Thanks for your control.
At runtime I'm trying to mark my node as being deleted before making changes.
I have to change the ImageIndex of the node to set appropriate icon indicating the node is being deleted. I set ImageIndex = 1 and SelectedImageIndex = 1, but as soon as the node loses focus ImageIndex returns back to 0 - default value for the tree.

So, how to set ImageIndex at runtime?

Thanks.
AnswerRe: ImageIndex is not working ? - Fixed Pin
Dmitry Dyachkov4-Aug-06 2:56
Dmitry Dyachkov4-Aug-06 2:56 
AnswerRe: ImageIndex is not working ? - Bug Pin
Mikael Wiberg4-Aug-06 8:05
Mikael Wiberg4-Aug-06 8:05 
QuestionBug:: MultiSameLevel Pin
highDensity18-Jul-06 1:24
highDensity18-Jul-06 1:24 
AnswerRe: Bug:: MultiSameLevel Pin
highDensity1-Aug-06 0:13
highDensity1-Aug-06 0:13 
QuestionWhat bout in VB.NET Pin
jo_eylee12-Jul-06 21:19
jo_eylee12-Jul-06 21:19 
QuestionMouse Cancel Events Pin
BulletSponge18-May-06 6:39
BulletSponge18-May-06 6:39 
AnswerRe: Mouse Cancel Events Pin
Mikael Wiberg19-May-06 17:11
Mikael Wiberg19-May-06 17:11 
QuestionSelected Nodes Pin
maheshcm13-Apr-06 20:12
maheshcm13-Apr-06 20:12 
AnswerRe: Selected Nodes Pin
Mikael Wiberg14-Apr-06 21:16
Mikael Wiberg14-Apr-06 21:16 
GeneralRe: Selected Nodes Pin
maheshcm14-Apr-06 23:36
maheshcm14-Apr-06 23:36 
GeneralRe: Selected Nodes Pin
Mikael Wiberg14-Apr-06 23:58
Mikael Wiberg14-Apr-06 23:58 
GeneralProviding Updates Pin
gingerlm1-Jun-05 2:39
gingerlm1-Jun-05 2:39 
GeneralRe: Providing Updates Pin
Mikael Wiberg1-Jun-05 2:49
Mikael Wiberg1-Jun-05 2:49 
GeneralMulti_Select Pin
sreejith ss nair24-May-05 2:05
sreejith ss nair24-May-05 2:05 
GeneralRe: Multi_Select Pin
Mikael Wiberg26-May-05 20:57
Mikael Wiberg26-May-05 20:57 
GeneralRe: Multi_Select Pin
sreejith ss nair26-May-05 21:27
sreejith ss nair26-May-05 21:27 
GeneralRe: Multi_Select Pin
Mikael Wiberg26-May-05 21:45
Mikael Wiberg26-May-05 21:45 

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.