CTreeCtrl won't SetCheck when tree isn't visible

I'm using a CTreeCtrl in a expandable dialog(it has a more/less button to show/hide options). The dialog is contracted to start out, which means the Tree is not visible(The visible property is set to true, it is just positioned outside the visible area of the window). When initialize the dialog, i populate the tree. The items get added correctly, and I call SetCheck(item, true) and this returns true, but when I expand the dialog to see the tree, the items are all unchecked.

Also once I have the dialog open, and add items to the tree, they show up as entries but they only show up checked if the tree is visible. Does SetCheck only work if the tree is visible or do I need to do something different.

Here's some code
                  POSITION p;
                  CTreeCtrl *rl = (CTreeCtrl*)GetDlgItem( IDC_TREE );
                  for(int i=0;i<m_list.GetCount();i++)
                  {
                        p = m_list.FindIndex(i);
                        if(p)
                        {
                              rImage img = m_list.GetAt(p);
                              HTREEITEM item = rl->InsertItem(img.name.c_str());
                              BOOL res = rl->SetCheck(item, img.checked);
                        }
                  }
tcf22Asked:
Who is Participating?
 
CetusMODConnect With a Mentor Commented:
PAQed - no points refunded (of 250)

CetusMOD
Community Support Moderator
0
 
Jaime OlivaresSoftware ArchitectCommented:
Have you tried with a fixed image? I am suspecting the problem is with the data pointed by p, and not with your control.
0
 
tcf22Author Commented:
What do you mean by "with a fixed image"?

p isn't the issue, it is just the POSITION of a struct in a CList.
These command get executed
                         HTREEITEM item = rl->InsertItem(img.name.c_str());
                         BOOL res = rl->SetCheck(item, img.checked);

The string (img.name.c_str()) is always added to the tree and the variable res always comes back true but the item that is added is unchecked reagardless of the value of img.checked.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
try GetDlgItem( IDC_TREE )->Invalidate(); after the resizing, it could be a painting problem
0
 
tonybluesCommented:
One solution is to call ModifyStyle to remove TVS_CHECKBOXES and then call ModifyStyle again to add TVS_CHECKBOXES back, before you call SetCheck. That is (in your sample):

CTreeCtrl *rl = (CTreeCtrl*)GetDlgItem( IDC_TREE );
rl->ModifyStyle(TVS_CHECKBOXES, 0);  // remove TVS_CHECKBOXES style
rl->ModifyStyle(0, TVS_CHECKBOXES);  // add TVS_CHECKBOXES style back
for(int i=0;i<m_list.GetCount();i++)
...


It really works. But I don't know why. It's just so strange!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.