Thanks to visit codestin.com
Credit goes to www.codeproject.com

65.9K
CodeProject is changing. Read more.
Home

Easy way to access listview from treeview and vice versa

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.18/5 (39 votes)

Jun 21, 2003

1 min read

viewsIcon

113291

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.