Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TreeView did not work

Posted on 1998-04-14
9
Medium Priority
?
345 Views
Last Modified: 2013-11-20
hi,
I try to implement a TreeView, but that did not work!
I bild a View class (CMyView) which inherits from CTreeView.
In the MS MFC samples I found a code to create a TreeCtrl in a TreeView class.
The creation von TreeCtrl is implemented int the "OnCreate" method:

int CObjView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CTreeView::OnCreate(lpCreateStruct) == -1)
        return -1;
#ifdef _MARCOC_OLD
    RECT rect ;
    rect.top = lpCreateStruct->y ;
    rect.left = lpCreateStruct->x ;
    rect.bottom = lpCreateStruct->cy ;
    rect.right = lpCreateStruct->cx ;

    if (!GetTreeCtrl().Create( WS_CHILD | WS_VISIBLE |
                                WS_BORDER |
                                TVS_LINESATROOT |
                                TVS_HASLINES |
//                                TVS_SHOWSELALWAYS |
                                TVS_HASBUTTONS |
                                TVS_DISABLEDRAGDROP,
                                rect, this, ID_TREEVIEW ))
    {
        TRACE( _T("Tree control failed to create!") ) ;
        return -1 ;
    }
#endif
    return 0;
}

I copied that method to CMyView. After that I tried to attach some items on
the tree in other methods. Something like this:

GetTreeCtrl().InsertItem("hello", TVI_ROOT, TVI_LAST );

The result: no TreeCtrl was created in my View.
The reason (I think): The TreeCtrl method "Create" is not called in "OnCreate" because of
_MARCOC_OLD is not defined. If I delete the #ifdef statement the produces an error
in wincore.cpp.

I tried the same with the MS MFC sample (OLEView) and it's the same behaviour!!!!!!
With the #ifdef statement the "GetTreeCtrl().Create(..." method is not called, because _MARCOC_OLD
is not defined. Without the statement the programm crashes!!

My question: How to implement a TreeView and attach items to it?
0
Comment
Question by:nil_dib
  • 5
  • 4
9 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 1600 total points
ID: 1317674
If you have view derived from CTreeView you do not need to create tree control, it is created internally by MFC. you have function GetTreeCtrl that returns reference to treecontrol associated with tree view.
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1317675
Ok,
but I tried also:

CTreeCtrl* m_pTree;
HTREEITEM m_hHitItem;

int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
      m_pTree = &GetTreeCtrl();      
...
}

void CMyView::MyMethod()
{
      m_hHitItem = m_pTree->InsertItem("hallo",TVI_ROOT, TVI_LAST );
      m_pTree->SetRedraw(TRUE);
}

and nothing is shown in MyView. In the debug session "m_hHitItem"
is 0 (InsertItem fails) !!!! So something must be wrong!

any comments?
0
 
LVL 7

Expert Comment

by:galkin
ID: 1317676
Try following

CTreeCtrl& m_Tree;
HTREEITEM m_hHitItem;

int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
m_Tree = GetTreeCtrl();
...
}


void CMyView::MyMethod()
{
m_hHitItem = m_Tree.InsertItem("hallo",TVI_ROOT, TVI_LAST );
m_Tree.SetRedraw(TRUE);
}

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:nil_dib
ID: 1317677
CTreeCtrl& m_Tree;

in my header produces a compiler error:
C2758 Initialisation must be in construcor

and:

void CMyView::MyMethod()
{
      CTreeCtrl &m_pTree = GetTreeCtrl();
      m_hHitItem = m_pTree.InsertItem("hallo",TVI_ROOT,TVI_LAST );
      m_pTree.SetRedraw(TRUE);
}
behaves like the code above: nothing shown, m_hHitItem = 0

more ideas?
0
 
LVL 7

Expert Comment

by:galkin
ID: 1317678
It is very amaizing. I just created test application using AppWizard and chose CTreeView as view. Then I added menu item to frame menu and cut and pasted you code into menu item handler. It woks fine! and m_hHitItem is NOT NULL
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1317679
you made something like this in the MainFrame?

void CMainFrame::OnTest()
{
  CTreeCtrl &m_pTree = ((CTreeView*)(GetActiveView( )))->GetTreeCtrl();
....

and inherits your View class from CTreeView?
0
 
LVL 7

Expert Comment

by:galkin
ID: 1317680
I added message handler to view, but it doesn't matter. I added message handler now to frame class and everything works as well.
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1317681
thats crazy,
I did the same ... and it didn't work.
Coulld you mail me your complete code ...
I will increase the points to 400.

mail: jds1@ipa.fhg.de

regards dib
0
 
LVL 4

Author Comment

by:nil_dib
ID: 1317682
ok ok I'm an idot...

BOOL COpAnalyser00View::PreCreateWindow(CREATESTRUCT& cs)
{
      return CView::PreCreateWindow(cs);
}
 
instead of

BOOL COpAnalyser00View::PreCreateWindow(CREATESTRUCT& cs)
{
      return CTreeView::PreCreateWindow(cs);
}

thanks a lot
dib



0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question