CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: enable button

  1. #1
    Join Date
    Mar 2000
    Posts
    2

    enable button

    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



  2. #2
    Join Date
    Jul 1999
    Location
    Uitca, NY
    Posts
    120

    Re: enable button

    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 );






  3. #3
    Join Date
    Jul 1999
    Posts
    14

    Re: enable button

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured