CTreeCtrl: best way to handle check boxes?

Posted on 2002-07-01
Last Modified: 2013-11-20
I have a tree control with a matching CTreeCtrl object contained in a CPropertyPage-derived class.  The tree control has check boxes using the appropriate tree control style flag.  The idea is that the tree control represents a directory structure of files and which ones you want to select to put in a zip archive.  If you select a folder, then all files in that folder should become selected and vice-versa.  Except that I also have some files that must be excluded but I want to show them in the tree anyway for recognition that the file exists and has been excluded by global user options other than unchecking its checkbox in the tree control.

I want to manage these check boxes by doing the following things:

- Some items should have the check box disabled.  If I can't directly do an EnableWindow(FALSE) kind of thing on that checkbox or tree item, then I'll have to implement it simply by unchecking the box whenever it becomes checked.  But what is the best way?

- If a group becomes checked, then I want to go through and check all of its child items (except for some that don't apply like the ones that must remain unchecked at all times, which is logic that I can handle).  Likewise, if the group becomes unchecked, then I want to go through and uncheck all of its child items.

- If an item is unchecked and none of its sibling items are checked, then I want to uncheck its parent item (if it has one) and so on up the tree if applicable

I can handle the tree relationships pretty easily.  I'm pretty familiar with that sort of thing.  What I'm most curious about is the notification events and architecture I should go for to implement the check boxes...

Is there an event or notification that is ONLY for the check box state change?  Or do I have to respond to every click (TVN_CLICK) on the tree control and then check the state of every item in the tree to see if something changed?  If so does that mean I need to keep a completely parallel copy of the data represented by the tree in order to do this comparison?

Is it better to handle these events in the CPropertyPage class that contains the tree cotnrol, or should I derive a class from CTreeCtrl and handle it all in there?

Is there a way to do a tri-state checkbox in a tree control?  And is there a way to EnableWindow(FALSE) or the equivalent on just one item of a tree control, or just one item's check box?  I want to know the best possible way to represent 4 states of items in the tree: unchecked, partly checked (contains some items that are checked and some that are not), fully checked (either doesn't have children or all children are also checked), and uncheckable (disabled).  If there's no way to do everything I want, I'd rather suffer the limitations of the built-in checkbox support in the tree control than create my own set of checkbox images and write my own checkbox handling code to duplicate that functionality on my own.  That is to say that if I can't get all 4 states that I want using the built-in checkbox support, I'll settle for the default two states (checked and unchecked) and work around that.
Question by:ris

Accepted Solution

mblat earned 100 total points
ID: 7123244
Best way to have 4 state checkbox I know of is not to use standard checkbox, but load different images for tree controll items depending on a state.  Images of cause can look like a check box...  It is pretty easy, so if 4 state is what you want - go this route and don't compromise with 2 state.

There are bunch of samples on how to do it here..

I personnaly ALWAYS prefer to derive class and handle this kind of situation in child control.  I find it much cleaner and easier to reuse later on...

I don;t think that you can disbale individual tree control item, but then again if you derive your class it is pretty easy to simulate.  See above for samples, especially ones that allow you to draw items in different colors.  You may also want to consider owner-draw tree control, even though it IS a lof of headache...

Hope it helps...

Expert Comment

ID: 7123842

About a year ago I implemented something quite similar to what you described here.
I used, like mblat suggested, a set of 5 images (in my case I also had a disabled-checked image), and implemented the hiererchical functionality myself.

I don't know of a way to do it using the basic tree ctrl check-boxes (not tri-state nor disable item).

If you want I can give you the code, but it will take some time to generalize it.

Author Comment

ID: 7124777
Thanks, that link was very helpful.  I guess that's what I'll do (use custom images).  From the examples, it looks less difficult than I thought it would be.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
building a service to accessed via telnet client, want colors ? 4 88
Thin secure Windows 10 5 93
Problem to App source 6 32
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now