Thread: Switching Default Buttons :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Switching Default Buttons :: MFC

    Hi.

    I would like to switch the style of two buttons to/from default. For example, initial ButtonA is the default button. As the user selects an option in a dialog box, ButtonB becomes the default button and buttonA becomes a normal button.

    I try changing the style of buttonB via SetStyle(BS_DEFBUTTON), however, that solution only highlights buttonB as though it is default. Nothing happens when I push Enter. When I push, Windows sends a message that buttonA is pushed. I set buttonA to default via Properties.

    Do you have to set the current default button to something else before setting another button to default?

    Thanks,
    Kuphry

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I don't use MFC, but I believe the "defaultness" moves with the focus. Something you might try is using the dialogs setfocus method to switch the focus to the desired new target.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Pryrates of CodeGuru posted a solution that works flawlessly. A special thanks to Pryrates for such an elegant solution.

    http://www.codeguru.com/forum/showth...hreadid=197179

    Here is the final code.

    -----
    void CDialog::SetDefaultBtnOk()
    {
    DWORD style = m_BtnOk.GetStyle();

    // make sure ButtonOK is not already the default button

    if ((style & BS_DEFPUSHBUTTON) != 1)
    {
    style = m_BtnClose.GetStyle();

    // remove default push button style
    style &= ~BS_DEFPUSHBUTTON;

    // set the style
    ::SendMessage(m_BtnClose.GetSafeHwnd(), BM_SETSTYLE, (WPARAM)style, (LPARAM)TRUE);

    // inform the dialog about the new default control id
    SendMessage(DM_SETDEFID, m_BtnOk.GetDlgCtrlID());

    // get the style
    style = m_BtnOk.GetStyle();

    // add the default push button style
    style |= BS_DEFPUSHBUTTON;

    // set the style
    ::SendMessage(m_BtnOk.GetSafeHwnd(), BM_SETSTYLE, (WPARAM)style, (LPARAM)TRUE);
    }
    }
    -----

    The second function SetDefaultBtnClose() is the compliment of the above.

    Kuphryn
    Last edited by kuphryn; 07-02-2002 at 04:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. how to change font of button's caption using MFC
    By urvashi in forum Windows Programming
    Replies: 5
    Last Post: 04-27-2004, 06:09 AM
  3. Enable/Disable Frame Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 6
    Last Post: 06-24-2002, 08:14 AM
  4. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  5. Problems with default buttons
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 05-24-2002, 01:31 PM