Click to See Complete Forum and Search --> : enable button


Marcelo
July 26th, 1999, 09:00 PM
Hello,

I have a SDI with a form. The form has a button on it which creates a modeless dialog when it is clicked.
After the user click on it I disabled the button. I would like to enable it when the user closes the dialog, but I can't make it work. My code is as follows:

I created a friend function of CView called EnableButtons and a CButton variable called mybutton. Then I am calling the function from the dialog. Until then everything works.
The code inside EnableButtons is:

CTestView* pButton;
pButton = new CTestView();
pButton->mybutton.EnableWindow(TRUE);

Then it compiles perfectly but when I click on the dialog button I get an error:

Debug Assertion Failed!

Could anybody please help me? Thanks,

Marcelo

John Killingbeck
July 26th, 1999, 11:33 PM
One way to handle this is to create a flag in the view class to tell if the dialog box is open. Initialize it to FALSE, then have the dialog box set it to TRUE upon opening and reset to FALSE on closing. Then in the view class create the UPDATE_COMMAND_UI function for the button's ID. Then in the OnUpdateButton() function put pCmdUI->Enable( TRUE ) to enable the button and pCmdUI->Enable( FALSE ) disable the button depending on the flag. ie.

// Disable button if dialog box open otherwise enable button
if( flag )
pCmdUI->Enable( FALSE );
else
pCmdUI->Enable( TRUE );

Milan Rathod
July 26th, 1999, 11:35 PM
Hi, Marcelo!

You are creating a new object of View in your
dialog box. But, it is not initialized. So,
even though it is having object of CButton (my button), that button doesnot have any window
attached to it.
Here you must understand that with each windowing
object (button, dialog box, edit box, window, ...)
there should be a window attached. Functions Create(), Attach(), DoModal() - do that job.

Now, your button object doesn't have any window
attached to it and you are calling EnableWindow(TRUE), that gives assertion.

And, again you are creating new view object
inside your dialog box, rather you should use
your existing view object.

for that you can change constructor to accept
view object

class CMyDialog : public CDialog
{
protected:
CMyView* myVi;
...
public:
CMyDialog(CMyView* view, /*other parameters,if
any */)
{
myVi = view;
...
}

OnMyButton()
{
myVi->mybutton.EnableWindow(TRUE);
}
}

When you are creating CMyDialog object from
your view class, pass 'this' as a parameter.

I hope you will understand this. And hope it will
work.

bye.
-milan