Solved

check/Uncheck  tree control in MFC using recursive

Posted on 2001-08-14
8
439 Views
Last Modified: 2013-11-20
i have a tree control with check boxes. i want to check or uncheck the tree control using recursive function.
how can i do this.
Requirements.
1. when i click on check box of an item all its parent should be checked.

2. when i double click on check box of an item all its child nodes has to be checked.

3. if an item has childrens then its single click event should not change the state of its item.

0
Comment
Question by:pratimag
8 Comments
 
LVL 2

Expert Comment

by:SamratAshok
ID: 6384325

you will need two separate recursive paths.

1st path : Check/uncheck parent of current element and recurse on parent.

2nd path : Check current element and loop for all its childs. (recurse at each child node)

When item is clicked, you travel on 1st path.

When item is double clicked, You will need to go to both paths and in this case
you have to ensure (via sentinel or thru global var) that items do not overlap

Needless to say, recursion is intuitive to think, but tricky to implement and also
tree control tends to present itself as a linked list, so looped alogirthms may
be more efficient.

0
 

Author Comment

by:pratimag
ID: 6387008
i have written a recursive function for this, but it is not working properly. if someone can send a sample code for this i will be greatful to him.

0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 6388156
Send in your code and I will tell you what you need to correct.

Are you/your design requirements flexible enough to implement
a non recursive implementation?

0
 

Author Comment

by:pratimag
ID: 6405476
Here is the code:

The description for parameters:
hParent - parent node of the item checked(in some cases it might be the grandparent node)

void CTestDialog::CheckParentStatus(HTREEITEM hParent,BOOL &bchkTreeStatus)
{    
     HTREEITEM hChildItem;
     CTreeCtrl* pDbgTree = (CTreeCtrl*)GetDlgItem(IDC_DEBUGGINGTREE);
     if(pDbgTree->ItemHasChildren(hParent))
     {
          hChildItem = pDbgTree->GetChildItem(hParent);
          while (hChildItem != NULL)
          {
               BOOL btemp = pDbgTree->GetCheck(hChildItem);
               if(btemp)
               {
                    if(pDbgTree->ItemHasChildren(hChildItem))
                    {
                              CheckParentStatus(hChildItem,bchkTreeStatus);
                    }
                    else
                    {
                         bchkTreeStatus = TRUE;
                    }
                    break;
               }
               else
               {
                    if(pDbgTree->ItemHasChildren(hChildItem))
                    {
                         CheckParentStatus(hChildItem,bchkTreeStatus);
                    }
                    else
                    {
                         bchkTreeStatus = FALSE;
                    }
               }
               hChildItem = pDbgTree->GetNextItem(hChildItem, TVGN_NEXT);
          }
     } // END of if loop for checking the children
     else
     {
          BOOL btemp = pDbgTree->GetCheck(hParent);
          if(btemp)
          {
               bchkTreeStatus = TRUE;
          }
          else
          {
               bchkTreeStatus = FALSE;
          }
          return;

     }
     
     if(bchkTreeStatus)
     {
          pDbgTree->SetCheck(hParent,TRUE);
          HTREEITEM hpItem;
          hpItem = pDbgTree->GetParentItem(hParent);
          while(hpItem != NULL)
          {
               pDbgTree->SetCheck(hpItem,TRUE);
               hpItem = pDbgTree->GetParentItem(hpItem);
          }
     }
     else
     {
          pDbgTree->SetCheck(hParent,FALSE);
          HTREEITEM hpItem;
          hpItem = pDbgTree->GetParentItem(hParent);
          while(hpItem != NULL)
          {
               pDbgTree->SetCheck(hpItem,FALSE);
               hpItem = pDbgTree->GetParentItem(hpItem);
          }
     }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:SamratAshok
ID: 6419770
Hi

I read the code,
I tried reproducing the error on my machine, by adding
a dialog box around your code, but could not do much
after that as I got swamped in my own work.

Shouldn't you divide functions that handle parents
and childs into separate functions and call each of them
exactly once.

Due to current design, for every child you will loop
over its parents. This although not infinite loop
is waste of time and complicated logic.

Few questions, what is the second param for? to return
the finally check value or value to set in first place.
You seem to be using same variable for multiple reasons.

if possible, Send me event code where you make this call!
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9521189
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

To be deleted and points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0
 
LVL 2

Expert Comment

by:SamratAshok
ID: 9524334
I had forgotten about this question. I think she used the solutions implied
and got on with it. I leave it to you to judge whether it is sufficiently
answered. In any case, I would request u to remove question from PAQ
list so that some other poor joe does not end up wasting points to see it.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9565786
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 122
Pressing cancel button with C# UI Automation 3 52
no14 challenge 14 62
Problem to open Excel file 15 99
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

20 Experts available now in Live!

Get 1:1 Help Now