Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Here is an MFC noob question.
I have a CTreeCtrl with TVS_CHECKBOXES set.
In OnInitDialog I populate the tree with a set of rules grouped into packages and groups, and I wish to preset a selection. The tree is populated ok but with no items checked. I can check items in the GUI and read that state ok, but my pre-sets are gone.

This is the code (well, parts of it):
OnInitDialog()
{
  __super::OnInitDialog();
  PopulateTree();
  return TRUE; 
}

PopulateTree ()
{
  mRulesTree.DeleteAllItems();

  // Aquire current info
  ParseRulesFile();

  // TBD: Do I need to do something like this? It is mentioned on the webb...
  mRulesTree.ModifyStyle( 0, TVS_CHECKBOXES );

  // Create the first nodes
  std::string package = mRulesList[0]->package;
  std::string group   = mRulesList[0]->group;
  HTREEITEM currPackage = mRulesTree.InsertItem( package.c_str(), NULL );
  HTREEITEM currGroup   = mRulesTree.InsertItem( group.c_str(), currPackage );
  HTREEITEM currRule;
  for ( size_t i = 0; i < mRulesList.size(); ++i )
  {
    if ( mRulesList[i]->package != package )
    {
      // Create new package node
      package = mRulesList[i]->package;
      currPackage = mRulesTree.InsertItem( package.c_str(), NULL );
    }
    if ( mRulesList[i]->group != group )
    {
      // Create new group node
      group = mRulesList[i]->group;
      currGroup = mRulesTree.InsertItem( group.c_str(), currPackage );
    }
    std::string ruleDesc = mRulesList[i]->name + " - " + mRulesList[i]->synopsis;
    currRule = mRulesTree.InsertItem( mRulesList[i]->name.c_str(), currGroup );
    if ( mSelectedRules.size() != 0 )
    {
      // We have a set of rules from the project settings.
      // If the current rule is in the list, check the checkbox.
      if ( std::find( mSelectedRules.begin(), 
                      mSelectedRules.end(), 
                      mRulesList[i]->name ) != 
             mSelectedRules.end() )
      {
        // TBD: DOESN'T WORK!
        mRulesTree.SetCheck( currRule, true );
      }
    }
    else
    {
      // We have no previous settings. Use default. TBD: DOESN'T WORK!
      mRulesTree.SetCheck( currRule, mRulesList[i]->default_on );
    }
  }
}


and this is from the resource file:
IDD_RULES_SETTINGS_DLG DIALOGEX 0, 0, 300, 300
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Rules Settings"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    LTEXT           "RULES",IDC_STATIC,7,8,32,8
    CONTROL         "",IDC_RULES_SETTINGS_TREE,"SysTreeView32",TVS_HASLINES | TVS_HASBUTTONS | TVS_CHECKBOXES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | WS_BORDER | WS_HSCROLL | WS_TABSTOP,14,20,273,245
    DEFPUSHBUTTON   "OK",IDOK,189,280,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,243,280,50,14
END


Regards
Robert
Posted
Updated 25-Nov-14 0:58am
v2
Comments
Jochen Arndt 25-Nov-14 7:04am    
You are trying to set the checks inside the loop when inserting items.
I'm not really sure but think that you can't do that while the tree is not full populated. You should try to set the checks using another loop afterwards.

it is some tricky. Microsoft writes in its documenation: "If you want to use this style, you must set the TVS_CHECKBOXES style with SetWindowLong after you create the treeview control, and before you populate the tree. Otherwise, the checkboxes might appear unchecked, depending on timing issues."

You better use it with SetWindowLong.
 
Share this answer
 
Comments
RobertM 25-Nov-14 10:52am    
Thank you for that pointer. I had seen someone mention the SetWindowLong before, but didn't understand what to do with it. Now I could a better search and found a solution.
With help of the input from KarstenK, some web searching gave me this elegant(?) solution of this stupid(!) problem:

Before populating the tree control in the PopulateTree function I do the following calls:
// First remove the checkbox style
mRulesTree.ModifyStyle( TVS_CHECKBOXES, 0 );
// Then explicitly set it
mRulesTree.ModifyStyle( 0, TVS_CHECKBOXES );


In my original solution I just did the latter call, and that was obviously not enough.

Thanks again!
Robert
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900