GetDocument in a dialog?
-
I have an MFC app and I want to use a dialog to filter data in the document. But how do I get hold of GetDocument()? All I can find that almost works is to use in OnInitDialog CTransistDoc* pDoc = (CTransistDoc*) CLeftPaneView::GetDocument(); ie I try to get it via my View, but this fails with error C2352: 'CView::GetDocument' : illegal call of non-static member function Which seems to be saying that OnInitDialog() is a static function. It is declared normally virtual BOOL OnInitDialog(); How do I sort this out? And where is this kind of nonsense explained? Thanks in advance for any help.
-
I have an MFC app and I want to use a dialog to filter data in the document. But how do I get hold of GetDocument()? All I can find that almost works is to use in OnInitDialog CTransistDoc* pDoc = (CTransistDoc*) CLeftPaneView::GetDocument(); ie I try to get it via my View, but this fails with error C2352: 'CView::GetDocument' : illegal call of non-static member function Which seems to be saying that OnInitDialog() is a static function. It is declared normally virtual BOOL OnInitDialog(); How do I sort this out? And where is this kind of nonsense explained? Thanks in advance for any help.
Mister Transistor wrote: CTransistDoc* pDoc = (CTransistDoc*) CLeftPaneView::GetDocument(); You are calling GetDocument as a static function (shown by the ::GetDocument). What you need to do here is get pointer to the class that contains the docuement, and call the GetDocument from that pointer. So it'd be something like this: CLeftPaneView *pLeftView; CTransistDoc *pDoc; CWnd* pWnd; pWnd = m_wndSplitter.GetPane(0, 1); pLeftView = DYNAMIC_DOWNCAST(CLeftPaneView, pWnd); pLeftView = pDoc->GetDocument(); This is how I get my document pointers in order to manipulate my docuement information. Hope this helps! :)
-
I have an MFC app and I want to use a dialog to filter data in the document. But how do I get hold of GetDocument()? All I can find that almost works is to use in OnInitDialog CTransistDoc* pDoc = (CTransistDoc*) CLeftPaneView::GetDocument(); ie I try to get it via my View, but this fails with error C2352: 'CView::GetDocument' : illegal call of non-static member function Which seems to be saying that OnInitDialog() is a static function. It is declared normally virtual BOOL OnInitDialog(); How do I sort this out? And where is this kind of nonsense explained? Thanks in advance for any help.
Create another constructor in your dialog class that accepts a CDocument pointer. Then construct your dialog object like:
CMyDialog dlg(this); dlg.DoModal();
Now your dialog class can communicate with the document class with the saved pointer. -
Create another constructor in your dialog class that accepts a CDocument pointer. Then construct your dialog object like:
CMyDialog dlg(this); dlg.DoModal();
Now your dialog class can communicate with the document class with the saved pointer. -
will1383, DavidCrow has a good idea, but remember that the default constructor for a dialog is:
CMyDialog(CWnd *pParent);
So you should create it like this:CMyDialog dlg(this);
The this parameter being a pointer to the parent window. It is good practice to always set the parent for a dialog. Don't create them like this:CMyDialog dlg;
If you do dialogs can sometimes not come to the top or fall behind other windows. So define your new constructor like so:CMyDialog(CWnd *pParent, CMyDoc *pDoc);
Now create your dialog like this:CMyDialog dlg(this, GetDocument());
Now you have the best of both. :) Jonathan Craig www.mcw-tech.com -
Mister Transistor wrote: CTransistDoc* pDoc = (CTransistDoc*) CLeftPaneView::GetDocument(); You are calling GetDocument as a static function (shown by the ::GetDocument). What you need to do here is get pointer to the class that contains the docuement, and call the GetDocument from that pointer. So it'd be something like this: CLeftPaneView *pLeftView; CTransistDoc *pDoc; CWnd* pWnd; pWnd = m_wndSplitter.GetPane(0, 1); pLeftView = DYNAMIC_DOWNCAST(CLeftPaneView, pWnd); pLeftView = pDoc->GetDocument(); This is how I get my document pointers in order to manipulate my docuement information. Hope this helps! :)
that's very interesting thank you ... I think you mean pDoc = pLeftView->GetDocument(); I'll try that shortly. Thanks again. Andrew