Search code examples
mfctreecontrol

How to change the selection from one item to the selected item in tree control?


I had a tree control which is being populated some values.And In this few tree nodes consists of check boxes whereas few doesn't. My problem is Initially when the tree is enumerated the default selection is on the first root node and this root node consists of three children where each child consists of check box. Here is my problem exactly is,

Now when I expand the tree and tried to click on the child item which consist of check box(clicking on the check box) Until the mouse left button down the focus comes to the item we are checking and when the left button click up then the selection is reverting back to the root item (or to the item which is previously selected).

In order to resolve this this is something I tried,

 //TVN_ITEMCHANGED 
void CDriverSetupView::OnTvnItemChangedTree(NMHDR *pNMHDR, LRESULT *pResult)
{
NM_TREEVIEW* pNewTreeView = (NM_TREEVIEW*)pNMHDR;

if(NULL != pNewTreeView->itemNew.hItem)
{
   m_TreeCtrl.Select(pNewTreeView->itemNew.hItem,TVGN_CARET | TVGN_FIRSTVISIBLE | TVGN_DROPHILITE); 
   m_TreeCtrl.SelectItem(pNewTreeView->itemNew.hItem);
   m_TreeCtrl.SelectDropTarget(pNewTreeView->itemNew.hItem);
}

}

If I am doing like this then I am able to get the selection to whatever the child I am checking everything is cool. But Initially when launch my application then the tree is is getting expanded and the selection is not on the root item ,it is moving on to the last item .

Please find the below images for better understanding, When the tree is enumerated without the piece of code in OnTvnItemChangedTree,the tree looks like this, enter image description here

Now when mouse left button down on the first checkbox the selection seems that it changed to checkbox item ,

enter image description here

Now when mouse left button up on the checkbox the selection again revert back to the previous node,

enter image description here

Now when I use the piece of code within OnTvnItemChangedTree then I am able to get the selection to the checked node but initially when I launch my application then the tree is getting expanded and the selection is on to the last child item which is as follows,

enter image description here

In the above image I am able to get what I want but the tree is getting expanded and the selection is on to the last item,I know this is because in the pNewTreeView->itemNew.hItem at last after the initialization that item consists handle to the last item ,but how can we make such that initially the tree should not get expanded and selection should properly work when I check on any node then the selection should get changed to the checked item.

Can anyone please let me know how can make the selection to be remained on to the root item and the tree Initial status should be as not expanded.


Solution

  • Finally I solved it .In order to avoid that expand while at the time Initialization and moving the focus from one item to the checked item can be achieved as follows,

    For NM_CLICK notification add this piece of code ,which works like charm.

    void CMyTreeCtrl::OnNMClickTree(NMHDR *pNMHDR, LRESULT *pResult)
    {
        if(NULL != pNewTreeView->itemNew.hItem)
        {
            m_TreeCtrl.Select(pNewTreeView->itemNew.hItem,TVGN_CARET | TVGN_FIRSTVISIBLE | TVGN_DROPHILITE); 
            m_TreeCtrl.SelectItem(pNewTreeView->itemNew.hItem);
        }
    
    }
    

    The only mistake I had done was added this code in OnTvItemChanged Instead I added in OnNMClickTree ,Now finally I achieved what exactly I want.