Solved

CTreeCtrl: best way to handle check boxes?

Posted on 2002-07-01
3
3,695 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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