Visual C++ 7.1Visual C++ 7.0Windows 2003Windows 2000Visual C++ 6.0Windows XPMFCIntermediateDevVisual StudioWindowsC++
Easy way to access listview from treeview and vice versa






2.18/5 (39 votes)
Jun 21, 2003
1 min read

113291
Easy way to access listview from treeview and vice versa in a Windows Explorer style application.
Introduction
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) toCLeftView
class with namem_treeview
. This will be of the shapeCTreeView * m_treeview
. - Add a public member variable (a pointer to
CListView
type) toCFView
class (where F is the name of the app.) with namem_listview
. This will be of the shapeCListView * m_listview
. - Add a public member function and which is
void
with nameGetViews()
to theCMainFrame
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 theCMainFrame::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.