Multi-Select TreeView Control in C#






4.88/5 (18 votes)
A TreeView control that implements multiple selection in different ways
Introduction
This latest version includes a multi-select TreeView
control which lets you choose a few different ways of selecting TreeNode
s.
There is a test project set up (MWControlsTest
) for testing all of the MWControls
.
MWControls
contains:
MWTreeView
: A Multi SelectTreeView
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
andRight
. MWLabel
: ALabel
control that usesStringFormat.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
andRight
.
Coding Technologies Used
MWTreeView
- Multi-select for
TreeNode
s. - Overridden
CancelEventArgs
so thatAfterXXX
andBeforeXXX
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 tofalse
- A graphical Design Time Editor inheriting from
UITypeEditor
is used for theTextDir
property
All
- Inheritance including overriding event handlers and programming around the fact that some methods, etc. in the parent control are hidden
Shortcomings
MWLabel
andMWScrollLabel
: TheUseMnemonic
property is not used. (I don't intend to fix this myself. If someone has a solution, please let me know.)MWTreeView
: WhenHotTracking
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 theMWTreeView
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 normalTreeView
Control.Multi
: Free unrestricted multi-selection - anyTreeNode
s can be selected.MultiSameBranchAndLevel
: MultipleTreeNode
s can only be selected if they are in the same branch and on the same level.MultiSameBranch
: MultipleTreeNode
s can only be selected if they are in the same branch.MultiSameLevel
: MultipleTreeNode
s can only be selected if they are on the same level.
- Several keys now do things to the
MWTreeView
: Ctrl+A selects allTreeNode
s (according to the restrictions imposed by theMultiSelect
property), etc, etc. TreeNode
s 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 thanNoMulti
, noTreeNode
has to be selected at all. and once one is selected, it can be deselected (control and mouse click). - The
FullRowSelect
property of the originalTreeView
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 theMWControls
. - Any new controls added to
MWControls
can appear on a newTabPage
.
Version 1.0.1.0
MWScrollLabel
- The
MWScrollLabel
is a subclassedLabel
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
andGenericTypographic
.GenericDefault
: This is theStringFormat
used by the originalLabel
control.GenericTypographic
: ThisStringFormat
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 textUpsideDown
: Horizontal text displayed upside downLeft
: Vertical text that can be read by tilting your head to the leftRight
: 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 theImageIndex
property) over the text or like the normalLabel
control under the text. - The image (the normal
Image
property or the image from theImageIndex
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.