Solved

TreeView did not work

Posted on 1998-04-14
9
333 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 400 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

943 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now