Click to See Complete Forum and Search --> : CTreeCnrl?


GL Hovis
March 30th, 1999, 06:14 PM
I set-up a message handler to handle a single click in a tree control. I'm trying to get the item text with a single

click. A member variable is associated with the tree (m_tree). A member variable is associated with an edit box in

the dialog (m_text).

The problem is, when I click on the first item nothing happens. When I click on the second item, the first item shows

up. Click on the third ... get the second, and so on. It seems that I'm not really selecting until another mouse

event click (two clicks on the same item will get that item's text).

How do I get the text of the item on the first click?

Here's the code I'm using.

void CMyDlg::OnClickTree1(NMHDR* pNMHDR, LRESULT* pResult)

{

HTREEITEM hItem=m_tree.GetSelectedItem();

if(hItem)

{

m_text=m_tree.GetItemText(hItem);

UpdateData(FALSE);

}


*pResult = 0;

}

Rudolf
March 30th, 1999, 11:17 PM
Your OnClick comes to soon, ie before the new selection takes place. Try the TVN_SELCHANGED message of CTreeCtl.

Hope for help

Rudolf

Vivek
April 1st, 1999, 01:05 PM
My guess is that you are handling the message TVN_SELCHANGING, which is sent when the selection in the tree is about to change. So in your handler, when you do a GetSelectedItem, you get a handle to the previous node. You should handle the TVN_SELCHANGED message instead, which will be sent after the selection in the tree control has changed.

B. Colombet
May 4th, 1999, 04:28 AM
The problem is that you get the selected item each time you click. Your click message handling is coming too soon and the selected item is always the previous one. Try to handle SEL_CHANGING message instead.
Another solution if you use MFC and a TreeView object is to get the mouse coordinates each time you click and ask the TreeCtrl which item is under the mouse.
void MyTreeView::OnClick(UINT Flags, CPoint point)
{
HTREEITEM hItem = TreeCtrl.HitTest(point);
// can be null
...
...
}

Hope it helps


C++ developer
Faculté de Médecine
27 Bd Jean Moulin
13385 Marseille cedex 05