How to enable buttons in my view ?
Hi everybody,
Here a strange problem for me.
In the OnInitialUpdate of my CView class, I disable buttons and control lists because no data is loaded.
When the user opens a file, in my CMyDoc::Serialize function, I tell my view to enable buttons. But it doesn't work. Look at this code.
void CMyDoc::Serialize(...)
{
if(!ar.IsStoring())
{
...
POSITION pos = GetFirstViewPosition();
CMyView* pView = (CMyView*) GetNextView(pos);
pView->EnableTheControls();
}
}
...
void CMyView::EnableTheControls()
{
CButton* pButton = (CButton*) GetDlgItem(IDC_BUTTON);
pButton->EnableWindow(TRUE);
}
The code below doesn't work, the controls are still disable. Can someone tell me why ? I try the UpdateWindow() function also in the CMyView function but I don't get anything more. Please help !
Re: How to enable buttons in my view ?
Put breakpoints and observe if they are getting called in the right sequence. Step through your code and verify that EnableTheControls() is being called.
Rating Helps!!
Re: How to enable buttons in my view ?
check if u have disabled the buttons in the base class and enabling them in the derived class.
Re: How to enable buttons in my view ?
The correct way to do this is to handle the OnSyncToDoc() message in your view. In OnSyncToDoc() you need to add your view to the document, and then this is where you will want to disable/enable your buttons.
LRESULT CYourClass::OnSyncToDoc(WPARAM wParam, LPARAM lParam)
{
CDocument* pNewDoc = lParam;
CDocument* pDoc = NULL;
BOOL bClosing = (BOOL)wParam;
//newdoc may be NULL
ASSERT_VALID(pNewDoc)
CDocument* pCurDoc = GetDocument();
if(pNewDoc)
//If we have a new doc use it.
pDoc = pNewDoc;
else
{
//Otherwize, use the current doc.
if(pCurDoc)
pDoc = pCurDoc;
}
if(bClosing)
{
//pNewDoc != pCurDoc if already changed to doc of another Eds4View
if(pCurDoc && (pNewDoc == pCurDoc || !pNewDoc))
{
pCurDoc->RemoveView(this);
UpdateToDoc();
}
EnableTheControls(FALSE) //Disable
}
else if(pNewDoc != pCurDoc)
{
if(pCurDoc)
pCurDoc->RemoveView(this);
if(pNewDoc)
{
pNewDoc->AddView(this);
EnableTheControls();
}
}
return 0; //result has no meaning
}
Hope that helps! Good luck.
Wade
I just understand the problem now
Well, I can understand why the controls can't be enabled in my program.
It's simply because I disable them in the OnInitialUpdate function of my view class. But I didn't know that this function is called each time a user opens a file. So, even if I enable them in another function, OnInitialUpdate is called after all and disable the controls.
Where can I initialize my controls if OnInitialUpdate is called not one time (as I thought first) but many times during the execution of the application ?
Thx.