Hello,
I have an SDI Doc/View application and I need to get access to the CFormView derived class to update some data in the view. Whats the best way to get a handle to the view class from outside that class?
Thanks!
Printable View
Hello,
I have an SDI Doc/View application and I need to get access to the CFormView derived class to update some data in the view. Whats the best way to get a handle to the view class from outside that class?
Thanks!
From anywhere inside your MFC app, you can call AfxGetMainWnd to get the main frame window. Next call the CFrameWnd methods GetActiveDocument or GetActiveView to get the doc or the view.
Actually, the best way (since this is a Doc/View architecture) is to store the data in your document. The view should be responsible for displaying that data to the user however needed.
Now when you need to change the data, change it in the document and then call the document class UpdateAllViews(). This will call the OnUpdate for each view, and the view can then redraw/update itself from the new document data. (Of course, you have to write the code in the OnUpdate handler to do whatever is needed.)
Hope that helps.
Hello,
There are many methods to do this.
Andreas Masur, in his FAQ MFC Doc/View: How to obtain a pointer to various objects gives a good compilation on how to get various objects from any part of the application. MSDN also gives good advice to get document and view from any where in the application in their article How To Get Current CDocument or CView from Anywhere. Another method is to create a variable of application class and initialise the variable with the view / document pointer in the respective constructor.
Regards,
Pravin.
Hello,
I tried the following code:
but it gives me an error for DisplayView.h:Code:CDisplayView* view = dynamic_cast<CDisplayView*>(((CFrameWnd*)AfxGetMainWnd())->GetActiveView());
error C2065: 'IDD_DISPLAY_FORM' : undeclared identifier
Any idea why I get this error?
Thanks!
The IDD_DISPLAY_FORM is a resource id to a dialog or a formview. I believe you just need to include resource.h in the file you are calling GetActiveView.