[RESOLVED] CButton and EnableWindow?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] CButton and EnableWindow?

  1. #1
    Join Date
    Aug 2010
    Posts
    47

    [RESOLVED] CButton and EnableWindow?

    This seems really simple. I want to disable a button. A simple google search tells me that I should be passing false to the button's EnableWindow function.

    It's not working, and I'm not sure why.

    Code snippet:
    Code:
    // Get the 'save' button
    CButton* saveButton = (CButton*)pMainFrame->m_toolBar.GetDlgItem(IDC_SAVE_BUTTON);
    saveButton->SetIcon(::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_SAVE_ICON))); // Works
    // The save button starts out disabled.
    saveButton->EnableWindow(FALSE); // Does not work
    // saveButton->ShowWindow(FALSE); // Works
    Now, I included a commented out 'showwindow' line in there which works just fine. It removes the button from the display. It's only there for testing purposes. The icon I put on the button with SetIcon also works just fine. I'm missing something dumb, aren't I?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: CButton and EnableWindow?

    Yes you are. You're doing it wrong. If you want to disable a toolbar button use the CCmdUI mechanism.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CButton and EnableWindow?

    You need to handle the CN_UPDATE_COMMAND_UI notification sent with WM_COMMAND. That means you need to add an entry like this to your message map:
    Code:
    ON_UPDATE_COMMAND_UI(IDC_SAVE_BUTTON, OnUpdateSaveButton)
    In your handler you should call Enable(FALSE),
    Code:
    void CMainFrame::OnUpdateSaveButton(CCmdUI* pCmdUI)
    {
        pCmdUI->Enable(condition);
    }
    where condition is a condition that evaluates to BOOL.

    Notice that OnUpdateSaveButton will be called (about) twice a second.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Aug 2010
    Posts
    47

    Re: CButton and EnableWindow?

    Quote Originally Posted by GCDEF View Post
    Yes you are. You're doing it wrong. If you want to disable a toolbar button use the CCmdUI mechanism.
    It's not a toolbar button, it's a regular CButton in a CDialogBar dialog. The m_toolBar is named that because the function of that CDialogBar is mainly to be a tool bar.

  5. #5
    Join Date
    Aug 2010
    Posts
    47

    Re: CButton and EnableWindow?

    Quote Originally Posted by cilu View Post
    You need to handle the CN_UPDATE_COMMAND_UI notification sent with WM_COMMAND.
    Well, I tried this out and it definitely works. Thanks!

    I'm still confused about why EnableWindow() doesn't work, though. Google for 'disable CButton' and the first five responses all say to use EnableWindow() to enable or disable it. Why does it presumably work for them, and doesn't work here?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: CButton and EnableWindow?

    A CDialogBar is derived from CControlBar, which is also the base class for CToolBar. That's the way they work.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,957

    Re: CButton and EnableWindow?

    I'm still confused about why EnableWindow() doesn't work, though. Google for 'disable CButton' and the first five responses all say to use EnableWindow() to enable or disable it. Why does it presumably work for them, and doesn't work here?
    The effect is because of special 'update UI' mechanism used in document-view framework which overrides native UI controls behavior. Whenever you switch to plain dialog-based app, you find EnableWindow starting to work all of a sudden.
    Best regards,
    Igor

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: CButton and EnableWindow?

    Quote Originally Posted by Igor Vartanov View Post
    The effect is because of special 'update UI' mechanism used in document-view framework which overrides native UI controls behavior. Whenever you switch to plain dialog-based app, you find EnableWindow starting to work all of a sudden.
    Not really. It's because that's how control bars work. EnableWindow works on CFormView and dialog controls and other windows in doc/view apps, just not in control bars.

  9. #9
    Join Date
    Aug 2010
    Posts
    47

    Re: CButton and EnableWindow?

    Visual C++ is chock full of these little 'gotchas', isn't it? Thanks for the help, guys.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: CButton and EnableWindow?

    Quote Originally Posted by Ankheg View Post
    Visual C++ is chock full of these little 'gotchas', isn't it? Thanks for the help, guys.
    That's not really Visual C++ per se - it's more like MFC.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center