Click here to Skip to main content
13,147,376 members (26,873 online)
Click here to Skip to main content
Add your own
alternative version

Stats

286.9K views
3.2K downloads
70 bookmarked
Posted 30 Apr 2003

Multi-Select TreeView Control in C#

, 15 Jun 2003
Rate this:
Please Sign up or sign in to vote.
A TreeView control that implements multiple selection in different ways.
<!-- Article image -->

Sample Image - mwcontrols03.gif

<!-- Add the rest of your HTML here -->

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

  • mikwib@hotmail.com (usual HoTMaiL spam filters)
  • mick@ar.com.au (heavy spam filters on, harldy anything gets through, start the subject with C# and it will probably go through)
  • md5mw@mdstud.chalmers.se (heavy spam filters on, harldy anything gets through, start the subject with C# and it will probably go through)

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

  • Updated, June 16th, 2003.

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

Share

About the Author

Mikael Wiberg
Web Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMulti_Select Pin
sreejith ss nair24-May-05 2:05
membersreejith ss nair24-May-05 2:05 
GeneralRe: Multi_Select Pin
Mikael Wiberg26-May-05 20:57
memberMikael Wiberg26-May-05 20:57 
GeneralRe: Multi_Select Pin
sreejith ss nair26-May-05 21:27
membersreejith ss nair26-May-05 21:27 
GeneralRe: Multi_Select Pin
Mikael Wiberg26-May-05 21:45
memberMikael Wiberg26-May-05 21:45 
QuestionCan we make Parent Icon larger than Child node icon? Pin
Anonymous27-Dec-04 0:46
sussAnonymous27-Dec-04 0:46 
GeneralNew version of the MWTreeView Pin
Mikael Wiberg16-Aug-04 18:35
memberMikael Wiberg16-Aug-04 18:35 
GeneralGreat article, good code comments Pin
Soliant15-May-04 21:01
memberSoliant15-May-04 21:01 
GeneralRe: Great article, good code comments Pin
Mikael Wiberg22-Aug-04 22:49
memberMikael Wiberg22-Aug-04 22:49 
Thanks for the nice comments.

Sorry I haven't answered your p.s. question (I missed it completely).

In order to paint a TextBox you don't have to create an owner drawn TreeView. Just put the TextBox on top of it. I don't really have much experience in doing this though, so I am probably not the best person to ask.

As for the paint event - it is missing.
I recommend the following article:
http://www.codeproject.com/cs/miscctrl/genmissingpaintevent.asp
QuestionIs their anyway to drag them? Pin
FocusedWolf5-May-04 7:06
memberFocusedWolf5-May-04 7:06 
AnswerRe: Is their anyway to drag them? Pin
Mikael Wiberg5-May-04 9:25
memberMikael Wiberg5-May-04 9:25 
AnswerRe: Is their anyway to drag them? Pin
Marco Tenuti26-Apr-05 0:50
memberMarco Tenuti26-Apr-05 0:50 
GeneralRe: Is their anyway to drag them? Pin
Mikael Wiberg17-May-05 4:22
memberMikael Wiberg17-May-05 4:22 
GeneralRe: Is their anyway to drag them? Pin
mtm@systematic.dk24-May-05 23:25
membermtm@systematic.dk24-May-05 23:25 
GeneralNice, now only if it was written in C++ Pin
Erik Thompson21-Apr-04 7:36
sitebuilderErik Thompson21-Apr-04 7:36 
GeneralRe: Nice, now only if it was written in C++ Pin
Mikael Wiberg21-Apr-04 9:12
memberMikael Wiberg21-Apr-04 9:12 
GeneralRe: Nice, now only if it was written in C++ Pin
Erik Thompson21-Apr-04 10:21
sitebuilderErik Thompson21-Apr-04 10:21 
GeneralSelecting node should select all descendants too Pin
Drew Noakes11-Mar-04 0:51
memberDrew Noakes11-Mar-04 0:51 
GeneralRe: Selecting node should select all descendants too Pin
Mikael Wiberg12-Mar-04 9:00
memberMikael Wiberg12-Mar-04 9:00 
GeneralRe: Selecting node should select all descendants too Pin
Mikael Wiberg12-Mar-04 10:57
memberMikael Wiberg12-Mar-04 10:57 
GeneralUpdates Pin
Drew Noakes11-Mar-04 0:43
memberDrew Noakes11-Mar-04 0:43 
GeneralRe: Updates Pin
Mikael Wiberg12-Mar-04 8:50
memberMikael Wiberg12-Mar-04 8:50 
GeneralRe: Updates Pin
alladum25-Aug-10 0:29
memberalladum25-Aug-10 0:29 
GeneralGreat TreeView, thanks =) Pin
ratzlaff22-Jan-04 14:58
sussratzlaff22-Jan-04 14:58 
GeneralRe: Great TreeView, thanks =) Pin
Mikael Wiberg24-Jan-04 2:48
memberMikael Wiberg24-Jan-04 2:48 
GeneralCan't programmatically check a node Pin
BrcKcc20-Jan-04 12:16
memberBrcKcc20-Jan-04 12:16 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 16 Jun 2003
Article Copyright 2003 by Mikael Wiberg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid