tabahi
tabahi

Reputation: 71

C++ Dialog Box TAB key not working

I have tried everything but couldn't get the TAB key working to move the focus from one control to another control in a resource dialog. Here is the code:

IDD_DLG_DIALOG DIALOGEX 0, 0, 219, 198
STYLE DS_SETFONT | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_EX_CONTROLPARENT
EXSTYLE WS_EX_APPWINDOW
CAPTION "Caption"
FONT 8, "Tw Cen MT", 400, 0, 0x0
BEGIN
        DEFPUSHBUTTON   "Done",IDOK,162,175,50,16
        EDITTEXT        IDC_EDIT1,27,13,185,12,ES_AUTOHSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
        PUSHBUTTON      "Add",IDC_Add,109,30,33,13,WS_TABSTOP | WS_VISIBLE | WS_CHILD
        LISTBOX         IDC_LIST1,7,49,205,121,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
        EDITTEXT        IDC_EDIT2,27,31,81,12,ES_AUTOHSCROLL | WS_TABSTOP | WS_VISIBLE | WS_CHILD
        LTEXT           "Name",IDC_STATIC,7,33,18,11
        LTEXT           "Link",IDC_STATIC,7,15,15,11    
        PUSHBUTTON      "Delete",IDC_DEL,144,30,33,13 | WS_TABSTOP | WS_VISIBLE | WS_CHILD
        CONTROL         "Autorun at startup",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,174,70,10 
        PUSHBUTTON      "Edit",IDC_EDIT,179,30,33,13,WS_TABSTOP | WS_VISIBLE | WS_CHILD
END

Main While loop:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdLine,int nCmdShow)
        {
            MSG msg;
            HACCEL hAccelTable;

            // Perform application initialization:
            if (!InitInstance (hInstance, nCmdShow)) return FALSE;
            hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_STEALTHDIALOG);
            while (GetMessage(&msg, NULL, 0, 0))
            {
            if((!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))||(!IsDialogMessage(msg.hwnd,&msg))) 
                        {
                            TranslateMessage(&msg);
                            DispatchMessage(&msg);
                        }
            }
    }

Please identify the problem here. Thanks.

Upvotes: 1

Views: 1176

Answers (2)

Jonathan Potter
Jonathan Potter

Reputation: 37192

Change your || to a &&. At the moment, if TranslateAccelerator returns 0 (which it will unless an accelerator key has been pressed), IsDialogMessage will never be called - and that's what handles the tab key.

(Additionally, as Marco A. implies in his answer, you should pass the HWND of your dialog, and not msg.hwnd as the first parameter of the IsDialogMessage call. But this is immaterial unless IsDialogMessage actually gets called, which it almost always won't while you have || instead of &&).

Upvotes: 1

Marco A.
Marco A.

Reputation: 43662

According to this post and confirmed by this kb article:

TranslateAccelerator() sends WM_COMMAND messages to the window whose handle you pass as an argument to TranslateAccelerator().

You are probably calling TranslateAccelerator() passing the hwnd member of your MSG structure; this will be the handle of the window that has the focus. You should pass the handle of your main windows instead.

And that applies to your code.

Upvotes: 0

Related Questions