Wesley Pattison Wesley Pattison - 3 months ago 23
C++ Question

DEFPUSHBUTTON not defaulting focus to IDCANCEL

My resource file contains a DEFPUSHBUTTON for IDCANCEL, but for some reason, they Focus on the button is not changing from IDOK to IDCANCEL.

IDD_HEALTHCARDINFO DIALOGEX 0, 0, 256, 223
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Health Card Information"
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
PUSHBUTTON "Yes",IDOK,67,202,50,14
DEFPUSHBUTTON "No",IDCANCEL,140,202,50,14


Inside my OnInitDialog() I've tried to force it by sending a DM_SETDEFID Message Also.

BOOL CHealthCardInfo::OnInitDialog()
{
CDialog::OnInitDialog();
SetDefID(IDCANCEL);
SendMessage(GetDlgCtrlID(), DM_SETDEFID, IDCANCEL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

Answer

Default button doesn't affect focus. It affects what button is activated by the ENTER key when some non-button control has focus.

You can use SetFocus in OnInitDialog (and also return FALSE) to change which control has focus when the dialog opens. Or you can reorder the controls within the dialog resource.


As a side note, a dialog box that asks a Yes/No question generally shouldn't have a title of "Information".

Comments