Solved

CTreeCtrl: best way to handle check boxes?

Posted on 2002-07-01
3
3,653 Views
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.
0
Comment
Question by:ris
3 Comments
 
LVL 4

Accepted Solution

by:
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..

http://codeguru.com/treeview/index.shtml

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...
0
 
LVL 1

Expert Comment

by:gurly
ID: 7123842
Hi,

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.
0
 
LVL 1

Author Comment

by:ris
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

12 Experts available now in Live!

Get 1:1 Help Now