Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easy way to access listview from treeview and vice versa

0.00/5 (No votes)
20 Jun 2003 2  
Easy way to access listview from treeview and vice versa in a Windows Explorer style application.

Introduction

Sample screenshot

This is an easy way to access the listview from treeview and vice-versa in a Windows Explorer Style MFC application. (It is specified in step number 5 of 6 in the MFC app wizard)

Let's say the project name is "F".

What is to be done ?

  • Create an SDI application with Windows Explorer style, with name "F".
  • Add a public member variable (a pointer to CTreeView type) to CLeftView class with name m_treeview. This will be of the shape CTreeView * m_treeview.
  • Add a public member variable (a pointer to CListView type) to CFView class (where F is the name of the app.) with name m_listview. This will be of the shape CListView * m_listview.
  • Add a public member function and which is void with name GetViews() to the CMainFrame class
  • Write the following code in the function:
    void CMainFrame::GetViews()
    {
    //Code beginning
    
     //get the right view (the list view)
    
     CWnd* pWnd = m_wndSplitter.GetPane(0, 1);
     //cast the right view to the CFView type where F is 
    
     //the name of Application and store it in list variable
    
     CFView* list= DYNAMIC_DOWNCAST(CFView, pWnd);
     //get the left view (the tree view)
    
     pWnd = m_wndSplitter.GetPane(0, 0);
     //cast it to CLeftView type and store it in tree variable
    
     CLeftView * tree = DYNAMIC_DOWNCAST(CLeftView, pWnd);
     //store the list view in the m_listview variable 
    
     //(member of CLeftView class)
    
     tree->m_listview=list;
     //store the tree view in the m_treeview 
    
     //variable (member of CFView class)
    
     list->m_treeview=tree;
     //end of code
    
    }
  • Add GetViews(); in the CMainFrame::OnCreate() function just before "return true;"

So how do I use that code

To access the ListView from Treeview,

  • On any event you want (click, double click, select) in the CLeftView class, add the following code:
    m_listview->GetListCtrl().InsertColumn(0,"Test");

To access the TreeView From ListView,

  • On any event you want (click ,double click, select) in the CFView class, add the following code:
    m_treeview->GetTreeCtrl().InsertItem("Test");

I hope that I helped.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here