Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / MFC
Article

Easy way to access listview from treeview and vice versa

Rate me:
Please Sign up or sign in to vote.
2.18/5 (39 votes)
20 Jun 20031 min read 112.5K   27   16
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:
    MC++
    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:
    MC++
    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:
    MC++
    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


Written By
Web Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood One Pin
Mohammad A Gdeisat25-Jan-05 8:51
Mohammad A Gdeisat25-Jan-05 8:51 
Generalerror in code corrected Pin
Douglas R. Keesler13-Nov-04 2:56
Douglas R. Keesler13-Nov-04 2:56 
GeneralNice article Pin
BaldwinMartin5-Nov-04 17:26
BaldwinMartin5-Nov-04 17:26 
GeneralRe: Nice article Pin
Douglas R. Keesler11-Nov-04 10:36
Douglas R. Keesler11-Nov-04 10:36 
GeneralHelpful Pin
eedwards11-Aug-03 11:41
eedwards11-Aug-03 11:41 
GeneralChild learning?! Pin
Lars [Large] Werner21-Jun-03 22:16
professionalLars [Large] Werner21-Jun-03 22:16 
GeneralRe: Child learning?! Pin
Oha Ooh24-Jun-03 10:23
Oha Ooh24-Jun-03 10:23 
GeneralRe: Child learning?! Pin
Lars [Large] Werner24-Jun-03 10:37
professionalLars [Large] Werner24-Jun-03 10:37 
GeneralRe: Child learning?! Pin
BaldwinMartin17-Nov-04 17:14
BaldwinMartin17-Nov-04 17:14 
GeneralRe: Child learning?! Pin
Lars [Large] Werner17-Nov-04 19:12
professionalLars [Large] Werner17-Nov-04 19:12 
GeneralRe: Child learning?! Pin
BaldwinMartin17-Nov-04 19:58
BaldwinMartin17-Nov-04 19:58 
GeneralRe: Child learning?! Pin
BaldwinMartin20-Nov-04 17:00
BaldwinMartin20-Nov-04 17:00 
GeneralThis is the worst article I have seen on CP Pin
WangYun21-Jun-03 18:48
WangYun21-Jun-03 18:48 
GeneralRe: This is the worst article I have seen on CP Pin
John M. Drescher21-Jun-03 21:17
John M. Drescher21-Jun-03 21:17 
GeneralRe: This is the worst article I have seen on CP Pin
Los Guapos22-Jun-03 17:17
Los Guapos22-Jun-03 17:17 
GeneralRe: This is the worst article I have seen on CP Pin
Gernot Frisch3-Sep-03 20:42
Gernot Frisch3-Sep-03 20:42 
I don't think so. It was exaclty what I was looking for and it got all the information required. Short, clear, compact.

Not good article maybe, but good information.
-Gernot

Searching for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.