CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: CTreeCnrl?

  1. #1
    Join Date
    Mar 1999
    Posts
    1

    CTreeCnrl?



    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;

    }

  2. #2
    Join Date
    Apr 1999
    Location
    Frankfurt, Germany
    Posts
    113

    Re: CTreeCnrl?



    Your OnClick comes to soon, ie before the new selection takes place. Try the TVN_SELCHANGED message of CTreeCtl.

    Hope for help

    Rudolf

  3. #3
    Join Date
    Apr 1999
    Posts
    4

    Re: CTreeCnrl?



    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.

  4. #4
    Join Date
    Apr 1999
    Location
    France
    Posts
    35

    Re: CTreeCnrl?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured