Click to See Complete Forum and Search --> : How can i access to a View from another view


ldeluca
May 27th, 1999, 09:48 AM
I have a question : How can i call a member's function of a view from another view without introduce personal pointers ????

Jim Watters
May 27th, 1999, 10:04 AM
get the doc from current view
from the doc traverse all views to get the type you want
cast it to that type
call the function


POSITION pos = pDoc->GetFirstViewPosition();

while (pos != NULL)
{
pView = pDoc->GetNextView(pos);
if (pView->IsKindOf(RUNTIME_CLASS(CView_List)))
{
((CView_List*)pView)->FooBar();
}
}

Dave Lorde
May 27th, 1999, 10:15 AM
You can iterate through all the views associated with a document using CDocument::GetFirstViewPosition() and CDocument::GetNextView(), which returns a CView*. So if the other view belongs to the same document, and you can identify it through its pointer, you can do it that way.

If the view belongs to another document, you can find it in a similar way by iterating the CDocTemplate, and you can iterate the CDocTemplates via the CWinApp...

But, an alternative way that fits the MFC Doc/View model rather better, is to do it indirectly by calling CDocument::UpdateAllViews(). This takes a pointer to the calling view, and two 'hint' arguments (you decide what they contain), and it calls OnUpdate() on every other view associated with the document, passing the caller view pointer and the hint arguments.

If you pass a hint value that only the target view will recognise, it can call the function you require in its OnUpdate() method.

This is the preferred technique for communicating between a document's views.

Dave