Check boxes in CTreeCtrl
-
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
-
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
Some code please - OnInitDialog handler would be best. :) Tomasz Sowinski http://www.shooltz.com.pl
-
Some code please - OnInitDialog handler would be best. :) Tomasz Sowinski http://www.shooltz.com.pl
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.
////////////////////////////////////////////////////////////////
// OnInitDialogBOOL 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
-
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.
////////////////////////////////////////////////////////////////
// OnInitDialogBOOL 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
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
-
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
G'day Tomasz, Thanks for that. It worked a treat! Steve