Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Check boxes in CTreeCtrl

Check boxes in CTreeCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Driessens
    wrote on last edited by
    #1

    G'day, I'm using a CTreeCtrl with check boxes on each item in a dialog. During to call to the dialog's OnInitDialog() I set up the initial checked/unchecked status for each tree item, but these checks seem to disapear when the dialog is actually displayed. I've poked around in my code and it seems that the checked status is actually lost for each tree item some time after OnInitDialog() returns. I can check/uncheck tree items with the mouse after the dialog is displayed, but it just seems to lose the checks when initializing. Can anybody point me in the right direction, please? I'm sure this has been discussed here at CP before, but I can't find any references. TIA, Steve

    T 1 Reply Last reply
    0
    • S Steve Driessens

      G'day, I'm using a CTreeCtrl with check boxes on each item in a dialog. During to call to the dialog's OnInitDialog() I set up the initial checked/unchecked status for each tree item, but these checks seem to disapear when the dialog is actually displayed. I've poked around in my code and it seems that the checked status is actually lost for each tree item some time after OnInitDialog() returns. I can check/uncheck tree items with the mouse after the dialog is displayed, but it just seems to lose the checks when initializing. Can anybody point me in the right direction, please? I'm sure this has been discussed here at CP before, but I can't find any references. TIA, Steve

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      Some code please - OnInitDialog handler would be best. :) Tomasz Sowinski http://www.shooltz.com.pl

      S 1 Reply Last reply
      0
      • T Tomasz Sowinski

        Some code please - OnInitDialog handler would be best. :) Tomasz Sowinski http://www.shooltz.com.pl

        S Offline
        S Offline
        Steve Driessens
        wrote on last edited by
        #3

        G'day Tomasz, I won't post my actual code here (it's way too long), but the following snippet has the same effect. Note, I've set up the tree control in the dialog as having check boxes enabled.

        ////////////////////////////////////////////////////////////////
        // OnInitDialog

        BOOL CReportSettingsDlg::OnInitDialog()
        {
        CDialog::OnInitDialog();

        CString strItemName;
        
            // Insert some dummy items into the tree...
        for(int i = 0; i < 20; i++){
        
        	strItemName.Format(\_T("Item%d"), i);
        	HTREEITEM hItem = m\_Tree.InsertItem(strItemName);
        	ASSERT(NULL != hItem);
        
                    // Set the check for the new item.
        	m\_Tree.SetCheck(hItem, TRUE);
        }
        
        return TRUE;
        

        }

        When the dialog above gets drawn, the tree displays the inserted items, but none of their check boxes are checked. TIA, Steve

        T 1 Reply Last reply
        0
        • S Steve Driessens

          G'day Tomasz, I won't post my actual code here (it's way too long), but the following snippet has the same effect. Note, I've set up the tree control in the dialog as having check boxes enabled.

          ////////////////////////////////////////////////////////////////
          // OnInitDialog

          BOOL CReportSettingsDlg::OnInitDialog()
          {
          CDialog::OnInitDialog();

          CString strItemName;
          
              // Insert some dummy items into the tree...
          for(int i = 0; i < 20; i++){
          
          	strItemName.Format(\_T("Item%d"), i);
          	HTREEITEM hItem = m\_Tree.InsertItem(strItemName);
          	ASSERT(NULL != hItem);
          
                      // Set the check for the new item.
          	m\_Tree.SetCheck(hItem, TRUE);
          }
          
          return TRUE;
          

          }

          When the dialog above gets drawn, the tree displays the inserted items, but none of their check boxes are checked. TIA, Steve

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          The solution is quite funny. Here's what I found in the "Tree View Control Window Styles" at MSDN: "TVS_CHECKBOXES: ... 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." To solve the problem, you have to: (1) clear the 'check boxes' option in the tree control properties in dialog editor (2) programatically set the style after the window is created and before items are inserted:

          m_Tree.ModifyStyle(0, TVS_CHECKBOXES);
          for (...)
          {
          m_Tree.InsertItem(...);
          m_Tree.SetCheck(...);
          }

          (2) is not enough (at least on my W2K box) - you can't have your tree control created with TVS_CHECKBOXES style. Cheers, Tomasz Sowinski http://www.shooltz.com.pl

          S 1 Reply Last reply
          0
          • T Tomasz Sowinski

            The solution is quite funny. Here's what I found in the "Tree View Control Window Styles" at MSDN: "TVS_CHECKBOXES: ... 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." To solve the problem, you have to: (1) clear the 'check boxes' option in the tree control properties in dialog editor (2) programatically set the style after the window is created and before items are inserted:

            m_Tree.ModifyStyle(0, TVS_CHECKBOXES);
            for (...)
            {
            m_Tree.InsertItem(...);
            m_Tree.SetCheck(...);
            }

            (2) is not enough (at least on my W2K box) - you can't have your tree control created with TVS_CHECKBOXES style. Cheers, Tomasz Sowinski http://www.shooltz.com.pl

            S Offline
            S Offline
            Steve Driessens
            wrote on last edited by
            #5

            G'day Tomasz, Thanks for that. It worked a treat! Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups