SAMPLE: ENTER & TAB Keys in Dialog Box Multiline Edit ControlLast reviewed: February 15, 1996Article ID: Q76474 |
The information in this article applies to:
SUMMARYIn an application running under Windows version 3.0 or 3.1, when the user interacts with a dialog box and presses the ENTER key, the application accepts any changes made to information in any control of the dialog box and dismisses the dialog box. However, when the dialog box contains a multiline edit control (MLE), it may be considered more intuitive for the user to use the ENTER key to advance to the next line in the edit control. Windows 3.0 and 3.1 use the CTRL+ENTER key combination for this purpose. Similarly, Windows uses the TAB key to move the input focus to the next control in the tabbing sequence in a dialog box. However, in an edit control, it might be useful for the user to be able to enter TAB characters. Windows uses the CTRL+TAB key combination for this purpose. Windows version 3.1 defines the ES_WANTRETURN edit control style, which changes the behavior of the ENTER key in an MLE in a dialog box. If an application is designed to run only under Windows versions 3.1 and later, specify the ES_WANTRETURN edit control style. None of the three techniques described below is necessary. This article discusses three different methods that can be used to modify the behavior of the ENTER key in an MLE in an application's dialog box. These methods are compatible with Windows versions 3.0 and 3.1. Methods 1 and 3 below can be adapted to modify the behavior of the TAB key, if desired. A major disadvantage of any of these methods is that the ENTER or TAB key will no longer act as it does in other applications, and its behavior no longer will be what an experienced Windows user would expect. MLEENTER is a file in the Microsoft Software Library that demonstrates two of the methods described in this document. Download MLEENTER.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:
MORE INFORMATIONUnder versions of Windows earlier than 3.0, an edit control subclass procedure could respond to the WM_GETDLGCODE message with DLGC_WANTALLKEYS to receive all keyboard input. This technique is not effective in Windows 3.0 or later.
METHOD 1This method involves subclassing the MLE. It is most useful when the edit control is already subclassed for another reason because the amount of additional code is minimal. This method involves three steps:
// In the subclass procedure switch (msg) { case WM_KEYDOWN: if (VK_RETURN == wParam) { PostMessage(hWnd, WM_CHAR, 0x0A, 0L); return 0L; } break; ... } return CallWindowProc(...);The disadvantage to subclassing is that it may be more fragile, with respect to changes in future versions of Windows, than other methods.
METHOD 2This method uses the DM_GETDEFID message that Windows sends to a dialog box procedure when the user presses the ENTER key in a dialog box. This method has the additional advantage of easily handling multiple multiline edit controls in the same dialog box and involves three steps:
static fEditFocus; switch (msg) { case WM_COMMAND: // ID_EDIT is a multiline edit control if (ID_EDIT == wParam) { if (EN_KILLFOCUS == HIWORD(lParam)) fEditFocus = FALSE; if (EN_SETFOCUS == HIWORD(lParam)) fEditFocus = TRUE; } else ... break; case DM_GETDEFID: /* * Check if an edit control has the focus and that * the ENTER key is down. DM_GETDEFID may be sent * in other situations when the user did not press * the ENTER key. */ if (fEditFocus && (0x8000 & GetKeyState(VK_RETURN))) { PostMessage(hEdit, WM_CHAR, 0x0A, 0L); return TRUE; } break; } return FALSE; METHOD 3Applications that use modeless dialog boxes are required to filter messages through the IsDialogMessage() function. The IsDialogMessage() function modifies certain messages to implement dialog box behavior. For example, the ENTER key message is modified to generate a WM_COMMAND message with wParam set to IDOK. An application is, however, free to modify the message before passing it to the IsDialogMessage() function. The code example below modifies the WM_KEYDOWN message containing a VK_RETURN to be a EM_REPLACESEL message with a carriage return (CR) and linefeed (LF) combination. The disadvantage to this method is that it places additional code in the main message loop for the application, slowing the processing of every message. In addition, code in the message loop is far removed from the dialog procedure, and is therefore harder to maintain. The following code fragment demonstrates this method:
/* * hWndEditControl is the handle to the multiline edit control. * hWndModeless is the handle to the modeless dialog box. */ while (GetMessage(&msg, NULL, 0, 0)) { if (hWndEditControl == msg.hWnd && WM_KEYDOWN == msg.message && VK_RETURN == msg.wParam) /* * Normally, Windows will translate this to IDOK. * Perform a custom translation to something more useful * (replace selection with a carriage return-linefeed). */ { msg.message = EM_REPLACESEL; msg.wParam = 0; msg.lParam = (long)(LPSTR)"\015\012"; } if (!IsDialogMessage(hWndModeless, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }If the functionality of a modal dialog box is desired in an application, it can be simulated by using a modeless dialog box that disables its parent window when the dialog is created.
|
Additional reference words: 3.00 3.10 softlib MLEENTER.EXE
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |