Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I created tri-state Ctreectrl in MFC, But while clicked on checkbox state is not updating on same instance.
For e.g. I checked on checkbox but by using GetItemState() i'm getting 1 for each item. If same item i'm unchecking i'm getting checked. So, It is returning Old state I trying to get new state. Can anybody help me out by some code snippet ?

What I have tried:

I checked on checkbox but by using GetItemState() i'm getting 1 for each item. If same item i'm unchecking i'm getting checked. So, It is returning Old state I am trying to get new state.

[EDIT by Jochen Arndt: Copied from comment]
C++
ITreeCtrl m_FiletreeCtrl = DEBUG_NEW ITreeCtrl;
HTREEITEM hItem = m_FiletreeCtrl->GetTreeCtrl().HitTest(point, &flags);
HTREEITEM hItem2 = m_FilestreeCtrl->GetTreeCtrl().GetParentItem(hItem);
HTREEITEM hChildItem = NULL;
while(hItem22 != NULL)
{
    hChildItem = m_FilestreeCtrl->GetTreeCtrl().GetChildItem(hItem22);
    while(hChildItem)
    {
        UINT nCurrentState = (m_FilestreeCtrl->GetTreeCtrl().GetItemState(hChildItem,TVIS_STATEIMAGEMASK)>>0xC);
    }
}

In this Tree I just want to know how many Items are checked, but GetItemState is returning Old state.
[/EDIT]
Posted
Updated 30-Nov-17 23:00pm
v3
Comments
Jochen Arndt 1-Dec-17 3:19am    
Where and when do you call GetItemState()?

Without seeing the used code it is impossible to help.
Rais Shaikh 1-Dec-17 4:18am    
ITreeCtrl m_FiletreeCtrl = DEBUG_NEW ITreeCtrl;
HTREEITEM hItem = m_FiletreeCtrl->GetTreeCtrl().HitTest(point, &flags);
HTREEITEM hItem2 = m_FilestreeCtrl->GetTreeCtrl().GetParentItem(hItem);
HTREEITEM hChildItem = NULL;
while(hItem22 != NULL)
{
hChildItem = m_FilestreeCtrl->GetTreeCtrl().GetChildItem(hItem22);
while(hChildItem)
{
UINT nCurrentState = (m_FilestreeCtrl->GetTreeCtrl().GetItemState(hChildItem,TVIS_STATEIMAGEMASK)>>0xC);
}
}

In this Tree I just want to know how many Items are checked, but GetItemState is returning Old state.

1 solution

Code from comment:
UINT nCurrentState = (m_FilestreeCtrl->GetTreeCtrl().GetItemState(hChildItem,TVIS_STATEIMAGEMASK)>>0xC);

You are not querying a check box state but the state image index. This index defines which image from the state image list is actually shown. By convention an index of zero indicates that there is no state image. An index of one (as detetected by you) is for the unchecked image. See also How to Work With State Image Indexes (Windows)[^].
 
Share this answer
 
Comments
Rais Shaikh 1-Dec-17 6:31am    
Thanks for solution. Then how i will get selected count from Tree.
Jochen Arndt 1-Dec-17 6:46am    
You are welcome and thank you for accepting my solution.

You have to iterate over the nodes starting at the root (or any defined node) and increment a count variable when an item is checked. Just search the web for something like "ctreectrl iterate" or "ctreectrl enumerate".

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