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

Thread: ctreectrl to clistbox

  1. #1
    Join Date
    Oct 2003
    Posts
    38

    ctreectrl to clistbox

    I want to convert my current tree to a list format.

    example
    Tree:
    +Group One
    |---One
    |---Two
    |---Three
    |---Four
    +Group Two
    |---A
    |---B
    |---C
    |---D

    to

    List:

    Group One
    One
    Two
    Three
    Four
    Group Two
    A
    B
    C
    D


    I am pretty sure i have to create a loop and call GetItemText(), but i am not sure how to properly do this.

    PS Please post an example (not that advanced programer).

  2. #2
    Join Date
    Nov 2002
    Location
    Lahore, Pakistan
    Posts
    52
    In CTreeCtrl u have a function GetNextItem with flag TVGN_ROOT

    insert this item in listbox or listctrl

    then call funcion ItemHasChildren it will return that either this item has child item's or not...

    if it has childern then call GetNextItem with flag TVGN_CHILD

    repeat this process untill u get NULL from GetNextItem

  3. #3
    Join Date
    Jul 2003
    Posts
    116

  4. #4
    Join Date
    Oct 2003
    Posts
    38
    so far i have got this code
    Code:
    
    HTREEITEM hItem, hRootItem, hNextItem;
    
    
    
    hRootItem = m_tree.GetNextItem(hItem,TVGN_ROOT);
    
    if (hRootItem)
    	m_list.AddString(m_tree.GetItemText(hRootItem));
    
    	if (m_tree.ItemHasChildren(hRootItem)){
    
    		while (hNextItem = m_tree.GetNextItem(hRootItem, TVGN_CHILD)){
    
    			m_list.AddString(m_tree.GetItemText(hNextItem));
    		}
    	}
    but m_tree.GetNextItem(hRootItem, TVGN_CHILD) is never returning null (making it a infinite loop)
    Last edited by the-griff; October 22nd, 2003 at 06:29 PM.

  5. #5
    Join Date
    Sep 2002
    Location
    14 39'19.65"N / 121 1'44.34"E
    Posts
    9,815
    Originally posted by the-griff
    but m_tree.GetNextItem(hRootItem, TVGN_CHILD) is never returning null (making it a infinite loop)
    Yes, because GetNextItem() with TVGN_CHILD will always return the first child of hRootItem. You should get only the first child, and then call GetNextItem() with TVGN_NEXT in your loop to get the next sibling.

  6. #6
    Join Date
    Oct 2003
    Posts
    38
    TVGN_NEXT also results in a infinite loop

  7. #7
    Join Date
    Sep 2002
    Location
    14 39'19.65"N / 121 1'44.34"E
    Posts
    9,815
    That's strange. Can you post your code? And BTW: If you use TVGN_NEXTVISIBLE, you don't need any recursion and changing between next child / next sibling: Just walk through your tree using TVGN_NEXTVISIBLE and starting at the root, and you will get a linear list of all items as they appear in the hierarchy.

  8. #8
    Join Date
    Oct 2003
    Posts
    38
    HTREEITEM hRootItem, hNextItem;
    hRootItem = m_tree.GetNextItem(hRootItem,TVGN_ROOT);
    m_list.AddString(m_tree.GetItemText(hRootItem));

    while (true){
    hNextItem = m_tree.GetNextItem(hRootItem, TVGN_NEXTVISIBLE );
    m_list.AddString(m_tree.GetItemText(hNextItem));

    if (!hNextItem)
    break;

    }

  9. #9
    Join Date
    Oct 2003
    Posts
    38
    K I got it semi working now:


    Code:
    void CHlmp3ListDlg::OnCreate() 
    {
    	m_list.ResetContent();
    	HTREEITEM hRootItem=NULL, hItem = m_tree.GetNextItem(hRootItem,TVGN_ROOT);
    
    	while (hItem != NULL)
    	{
    		
    		if (m_tree.GetCheck(hItem)){
    			m_list.AddString(m_tree.GetItemText(hItem));
    		}
    
    		hItem = m_tree.GetNextItem(hItem,TVGN_NEXTVISIBLE);
    	}
    
    }
    but it will only add the string if the item is checked and it is visiablly expanded.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)