Thread: More than one DEFPUSHBUTTON

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    More than one DEFPUSHBUTTON

    I was wondering wether it's possible to have more than one DEFPUSHBUTTON on a dialog.
    I currently have a dialog with two sections. Each section has a edit control and a defpushbutton. But when an edit control has the focus and the user presses enter, it's always the same button that gets "clicked". I want to know if it's possible for "Button1" to get clicked when "Edit1" has the focus (and enter has been pressed) and "Button2" to get clicked when "Edit2" has the focus.

    Thanks

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    It is, but it isn't simple. The whole point of "DEFPUSHBUTTON" is it being "the default push button", and you obviously can't have more than one default. What you'd need to do is manually capture enter presses on each edit box and decide which button to send the click message to there.

    In other words, I hope you enjoy subclassing...

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Maybe there's a way to let the dialog know which button is the current default button? I now have this code, but it only changes the looks of the buttons. Apparently a button with BS_DEFPUSHBUTTON set still isn't a real default button for the dialog...

    Code:
    case WM_COMMAND:
        if(HIWORD(wParam) == EN_SETFOCUS) {
            switch(LOWORD(wParam)) {
                case ID_JOIN_IP:
                case ID_JOIN_PORT:
                    SendDlgItemMessage(hwnd, ID_CREATE, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
                    SendDlgItemMessage(hwnd, ID_JOIN, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
                    break;
                case ID_CREATE_PORT:
                    SendDlgItemMessage(hwnd, ID_JOIN, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
                    SendDlgItemMessage(hwnd, ID_CREATE, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
                    break;
            }
        }
    I'll go with subclassing for now, but maybe someone knows wether it's possible to let the dialog know which button is the default one.
    Last edited by Snip; 07-18-2005 at 07:22 AM.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Looking at this again, I've got an idea.

    One of the buttons with the BS_DEFPUSHBUTTON style is triggered when you press enter on an edit box, yes? What you could do therefore is determine which edit box has the keyboard focus (by processing the EN_SETFOCUS notification through the parent's WM_COMMAND) and change the buttons styles using SetWindowLong so that only one (the right one) is the default.

    It's so long-winded and insane, it might just work...

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Apparently a button with BS_DEFPUSHBUTTON set still isn't a real default button for the dialog...
    You need to send DM_SETDEFID to your dialog as well.

    SMurf's method should work too

    gg

Popular pages Recent additions subscribe to a feed