skyhook8
asked on
Using CTreeCtrl From A Derived Class
I've created an Explorer like application using MFC wizard. I then created a generic class which is derived from CLeftView. I can then add this code -
CTreeCtrl &pTree = GetTreeCtrl();
pTree.ModifyStyle(0, TVS_HASBUTTONS | TVS_HASLINES, SWP_SHOWWINDOW);
to a function in the new class which will compile okay but when I run it and access the function I get "Debug Assertion Failed!".
Is it possible to control a tree from any other class than CLeftView and if so how?
Thanks in advance.
CTreeCtrl &pTree = GetTreeCtrl();
pTree.ModifyStyle(0, TVS_HASBUTTONS | TVS_HASLINES, SWP_SHOWWINDOW);
to a function in the new class which will compile okay but when I run it and access the function I get "Debug Assertion Failed!".
Is it possible to control a tree from any other class than CLeftView and if so how?
Thanks in advance.
Also,
Where are you modifying the style - are you doing this in the constructor? If so, the tree ctrl hasn’t been created at this point. You should be modifying this style in the function PreCreateWindow(…)
Something like
CleftViewDerivation::PreCr eateWindow (CREATESTR UCT& cs)
{
cs.style |= TVS_HASBUTTONS | TVS_HASLINES;
return CleftView::PreCreatewindow (cs);
}
Where are you modifying the style - are you doing this in the constructor? If so, the tree ctrl hasn’t been created at this point. You should be modifying this style in the function PreCreateWindow(…)
Something like
CleftViewDerivation::PreCr
{
cs.style |= TVS_HASBUTTONS | TVS_HASLINES;
return CleftView::PreCreatewindow
}
ASKER
You guys are responding faster than I can try your ideas!
I added the "if(pTree)" and it runs without crashing now but nothing shows up in the left pane.
void CTreeEx::PutTree()
{
CTreeCtrl &pTree = GetTreeCtrl();
if(pTree)pTree.ModifyStyle (0, TVS_HASBUTTONS | TVS_HASLINES, SWP_SHOWWINDOW);
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = NULL;
tvinsert.item.mask = TVIF_SELECTEDIMAGE | TVIF_TEXT;
tvinsert.item.pszText = "Org";
if(pTree)HTREEITEM hRoot = pTree.InsertItem(&tvinsert );
return;
}
This is being called from OnInitialUpdate() in CLeftView.
I added the "if(pTree)" and it runs without crashing now but nothing shows up in the left pane.
void CTreeEx::PutTree()
{
CTreeCtrl &pTree = GetTreeCtrl();
if(pTree)pTree.ModifyStyle
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = NULL;
tvinsert.item.mask = TVIF_SELECTEDIMAGE | TVIF_TEXT;
tvinsert.item.pszText = "Org";
if(pTree)HTREEITEM hRoot = pTree.InsertItem(&tvinsert
return;
}
This is being called from OnInitialUpdate() in CLeftView.
Can you show us the OnInitialUpdate() code. Is this a dialog or form?
HI,
Try modifying ur PutTree() code as
void CTreeviewView::PutTree()
{
CTreeCtrl *pTree = &(this->GetTreeCtrl());
if(pTree)
{
long wd = GetWindowLong(pTree->GetSa feHwnd(), GWL_STYLE);
wd += TVS_HASBUTTONS + TVS_HASLINES;
SetWindowLong(pTree->GetSa feHwnd(), GWL_STYLE, wd );
}
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = TVI_ROOT ;
tvinsert.hInsertAfter = TVI_LAST;
tvinsert.item.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_CHILDREN | TVIF_STATE | TVS_CHECKBOXES;
tvinsert.item.hItem = NULL;
tvinsert.item.state = 0;
tvinsert.item.stateMask = 0x03;
tvinsert.item.cchTextMax = 60;
tvinsert.item.iSelectedIma ge = 0;
tvinsert.item.cChildren = 1;
tvinsert.item.lParam = 0;
tvinsert.item.pszText = "NUMBERS";
HTREEITEM Par1 ;
if(pTree)
Par1 = pTree->InsertItem(&tvinser t);
tvinsert.hParent = Par1 ;
tvinsert.item.cChildren = 0;
tvinsert.item.pszText = "child";
if(pTree)
HTREEITEM child = pTree->InsertItem(&tvinser t);
return;
}
Try it out.
VinExpert
Try modifying ur PutTree() code as
void CTreeviewView::PutTree()
{
CTreeCtrl *pTree = &(this->GetTreeCtrl());
if(pTree)
{
long wd = GetWindowLong(pTree->GetSa
wd += TVS_HASBUTTONS + TVS_HASLINES;
SetWindowLong(pTree->GetSa
}
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = TVI_ROOT ;
tvinsert.hInsertAfter = TVI_LAST;
tvinsert.item.mask = TVIF_TEXT | TVIF_SELECTEDIMAGE | TVIF_IMAGE | TVIF_CHILDREN | TVIF_STATE | TVS_CHECKBOXES;
tvinsert.item.hItem = NULL;
tvinsert.item.state = 0;
tvinsert.item.stateMask = 0x03;
tvinsert.item.cchTextMax = 60;
tvinsert.item.iSelectedIma
tvinsert.item.cChildren = 1;
tvinsert.item.lParam = 0;
tvinsert.item.pszText = "NUMBERS";
HTREEITEM Par1 ;
if(pTree)
Par1 = pTree->InsertItem(&tvinser
tvinsert.hParent = Par1 ;
tvinsert.item.cChildren = 0;
tvinsert.item.pszText = "child";
if(pTree)
HTREEITEM child = pTree->InsertItem(&tvinser
return;
}
Try it out.
VinExpert
sorry - ignore my bit about is this a dialog or form.
But I would still like to see where you are trying to modify the tree style. The reason why it's not asserting anymore is simply because that code is now no longer being called - which really doesn't help you much...
But I would still like to see where you are trying to modify the tree style. The reason why it's not asserting anymore is simply because that code is now no longer being called - which really doesn't help you much...
ASKER
Tried the new code. Getting a "Debug Assertion Failed!" again. This line causes the problem:
if(pTree)
Par1 = pTree->InsertItem(&tvinser t);
Here's the OnInitialUpdate() code:
void CLeftView::OnInitialUpdate ()
{
CTreeView::OnInitialUpdate ();
CTreeEx te;
te.PutTree();
}
Ideas?
if(pTree)
Par1 = pTree->InsertItem(&tvinser
Here's the OnInitialUpdate() code:
void CLeftView::OnInitialUpdate
{
CTreeView::OnInitialUpdate
CTreeEx te;
te.PutTree();
}
Ideas?
skyhook, I think you might be a little confused about what you trying to achieve and how to go about it using OOP and MFC.
What exactly are you trying to do? Are you just trying to modify the style of the CLeftView class?
What exactly are you trying to do? Are you just trying to modify the style of the CLeftView class?
ASKER
Adjusted points to 70
ASKER
I'm attempting to add items to the tree in the left window pane using "void CTreeEx::PutTree()".
I can do it easily from "void CLeftView::OnInitialUpdate ()" but when I call the previous function from the latter I get into trouble. CTreeEx is derived from CLeftView.
There's no doubt that I'm confused. But I've come a long way and I don't intend to stop here.{:>
The "rowlist" example from MSDN adds items to a list from a derived class but I can't figure out why it works.
I can do it easily from "void CLeftView::OnInitialUpdate
There's no doubt that I'm confused. But I've come a long way and I don't intend to stop here.{:>
The "rowlist" example from MSDN adds items to a list from a derived class but I can't figure out why it works.
Okay, well your trouble is OOP in general I think :-) For the time being I suggest you forget entirely about your derived CLeftTree (CTreeEx).
What you should do to get things working is first override PreCreateWindow() in CLeftTree like the following:
BOOL CLeftView::PreCreateWindow (CREATESTR UCT& cs)
{
cs.Style |= TVS_HASBUTTONS | TVS_HASLINES;
return CTreeView::PreCreateWindow (cs);
}
This will get the styles you want. Then in OnInitialUpdate(..) add the items to the tree.
As far as I can tell, you have no need to derive a class from CLeftTree and if you did, you go about it in a different way entirly.
What you should do to get things working is first override PreCreateWindow() in CLeftTree like the following:
BOOL CLeftView::PreCreateWindow
{
cs.Style |= TVS_HASBUTTONS | TVS_HASLINES;
return CTreeView::PreCreateWindow
}
This will get the styles you want. Then in OnInitialUpdate(..) add the items to the tree.
As far as I can tell, you have no need to derive a class from CLeftTree and if you did, you go about it in a different way entirly.
ASKER
I'm going to be calling ldap lookups from the derived class and I want to be able to add items to the tree while in a ldap_next_entry loop. I could do the loop from within CLeftView but I figured that a separate class for the ldap functions would be a more organized way of doing things.
Does that make sense?
Does that make sense?
I guess Scotty is right in saying that your problem is actually OOP in general.
Because...the very code you have written works on my machine.
The situation...SDI, two panes, left CMyTree(derived from CTreeView) and right CMyListView(from CListView).
CMyTreeView::::OnInitialUp date()
{
CTreeView::OnInitialUpdate ();
PutTree();
}
PutTree is your function. Works well.
Luck,
Waseem.
Because...the very code you have written works on my machine.
The situation...SDI, two panes, left CMyTree(derived from CTreeView) and right CMyListView(from CListView).
CMyTreeView::::OnInitialUp
{
CTreeView::OnInitialUpdate
PutTree();
}
PutTree is your function. Works well.
Luck,
Waseem.
Hi,
From InitialUpdate just call PutTree();
OR
do one more thing(of which I am not so comfertable)
In InitialUpdate() set the timer as
SetTimer(1, 10, NULL);
Then map the WM_TIMER message and do
KillTimer(1);
PutTree();
Try it out.
VinExpert
From InitialUpdate just call PutTree();
OR
do one more thing(of which I am not so comfertable)
In InitialUpdate() set the timer as
SetTimer(1, 10, NULL);
Then map the WM_TIMER message and do
KillTimer(1);
PutTree();
Try it out.
VinExpert
What is it that you are actually trying to suggest VinExpert...I guess the guy already is in a soup....
Waseem.
Waseem.
Hi,
I am sorry skyhook8, if I am confusing U. But I thought to put it.
VinExpert
I am sorry skyhook8, if I am confusing U. But I thought to put it.
VinExpert
ASKER
Okay, let's start over:
Using MFC AppWizard (exe), create an Explorer like application. Now create a new class and then from that class add tree items to the left pane.
Is it possible? Details please.
BTW: Thanks to all for your time and patience.
Using MFC AppWizard (exe), create an Explorer like application. Now create a new class and then from that class add tree items to the left pane.
Is it possible? Details please.
BTW: Thanks to all for your time and patience.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Eureka! Thanks so much!
For archival purposes, please note the change I had to make to OnInitialUpdate():
void CLeftView::OnInitialUpdate ()
{
CTreeView::OnInitialUpdate ();
CTreeCtrl& TreeCtrl = GetTreeCtrl();
CTreeHelper CTreeHelper(&TreeCtrl);
CTreeHelper.Populate();
}
Thanks again and everyone please forgive me for not making my question clearer from the beginning.
For archival purposes, please note the change I had to make to OnInitialUpdate():
void CLeftView::OnInitialUpdate
{
CTreeView::OnInitialUpdate
CTreeCtrl& TreeCtrl = GetTreeCtrl();
CTreeHelper CTreeHelper(&TreeCtrl);
CTreeHelper.Populate();
}
Thanks again and everyone please forgive me for not making my question clearer from the beginning.
Ooops yes - sorry I knocked it up hastily.
I assume u r doing it in Ur derived class. Then u r not checking for the validity of pointer, thats why it is crashing do as
CTreeCtrl &pTree = GetTreeCtrl();
if(pTree)
pTree.ModifyStyle(0, TVS_HASBUTTONS | TVS_HASLINES, SWP_SHOWWINDOW);
That will solve the crash.
VinExpert