Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INTRODUCTION AND RELEVANT INFORMATION:

I need to implement the following scenario:

1. User expands a node, e.g. Node 1;

2. User expands another node, e.g. Node 2;

3. Collapse previous node ( Node 1 );

To visually explain what I mean, we shall use the following example image[^].

Now when user clicks on Assembly 1 node or its child node Components, I need to collapse every other node. Example of what I mean is illustrated here[^].

MY EFFORTS TO IMPLEMENT THIS BEHAVIOR:

Browsing through Internet, and with a little thinking of my own, I was able to write helper function that collapses node and its children :

C++
void CollapseNode( HWND hTree, HTREEITEM hti )
{
    if( TreeView_GetChild( hTree, hti ) != NULL )
    {
        TreeView_Expand( hTree, hti, TVE_COLLAPSE );
        hti = TreeView_GetChild( hTree, hti );
        do
        {
            CollapseNode( hTree, hti );
        }
        while( ( hti = TreeView_GetNextSibling( hTree, hti ) ) != NULL  );
    }
}


Reading through MSDN documentation I found TVN_ITEMEXPANDING and TVN_ITEMEXPANDED messages that might be useful.

I have also found NM_CLICK notification that seems interesting since I can use TVM_HITTEST message to test if click was on +/- button.

Also I have found TVN_KEYDOWN message that will help me with expanding when user presses left arrow or right arrow keys.

PROBLEM:

I can not think of an algorithm for using the above messages to solve my task.

QUESTION:

Can you please suggest me an algorithm for handling the above messages so I can call my CollapseNode(..) function ?

Something like this:

Hittest in NM_CLICK and then call your function or Store the last expanded item in a variable and collapse it in response to TVN_ITEMEXPANDED would be good for a start.

Thank you.
Posted
Comments
Code-o-mat 16-May-14 12:16pm    
Did you try adding the TVS_SINGLEEXPAND (see here: http://msdn.microsoft.com/en-us/library/bb760013.aspx) style to your tree control? In case you try it, note that according to the documentation linked above, it will still allow the user to have multiple items expanded by using the Control key.
AlwaysLearningNewStuff 17-May-14 17:32pm    
I have tried with that style as well, but the behavior is not exactly the one I want.

Thank you for your help.

1 solution

hi,
Used Tree control long long ago so i don't know if what i am going to tell you is already implemented some how. Also this might not be the best approach. But it might work well.
In tree traversal the uniquness of the path is important.
Implement a OnTvnSelchangedTree event.
In this event get a handle to the item selected by user .
Now using the handle, you can get the text of the item user clicked on . Start a vector of strings and store this string in that vector(You are making the unique path here which will come in handy later).
Also using the handle get a handle to this selected items parent and when you have the handle to the parent, get the text on parent.Store this in the vector of strings .
Keep doing this until you reach the root of the tree .So, finally you will have a vector which has the unique path right from root up to the node the user selected.
For example the vector will have <test assembly="" component="" 3=""> as unique path if user selected Component 3.

Now call your collapse function to collapse the whole tree.
Implement a function to expand the tree nodes.(You implemented one for collapse so this should not be much trouble for you ;))
Call your expand function.
The call to both collapse and expand functions will be from inside the OnTvnSelchangedTree event itself.
Now in this expand function at each node get the text on the node and check it against the vector of strings that you have. That is to say, expand the root of the tree.
the string corresponding to the root was first element of the vector. Now get all children of the root but call the expand function only for the one which has the same text as the second element in your vector. Then get its children and expand only the one which has the same text as the third element of vector ... and so on.
I hope you do get the big picture. Basically maintaining the path is the only crucial part.

I am not sure if for maintaining the unique path, instead of storing the text on the items, we could just store the handles to the items themselves. You can give it a try.
Make use of MSDN and Google. GetSelectedItem, GetItemText and GetParentItem etc are already provided.
Good luck ...and please let me know how it goes ;)
 
Share this answer
 
Comments
AlwaysLearningNewStuff 15-May-14 4:18am    
Thank you for helping. I will try to implement your method and report results as soon as possible. I will need some time though...

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