PRB:Unselecting Edit Control Text at Dialog Box InitializationLast reviewed: November 2, 1995Article ID: Q96674 |
The information in this article applies to:
SYMPTOMSTo remove the highlight (selection) from an edit control text, an EM_SETSEL message must be sent to the control. However, while processing the WM_INITDIALOG message of a dialog box, sending an EM_SETSEL fails to remove the highlight from (unselect) the edit control text.
CAUSEWhile processing the WM_INITDIALOG message, sending the EM_SETSEL message fails to remove the highlight from the edit control. This happens because the edit control has not yet been drawn. Because it's not drawn and there is no selection information available to the edit control's procedure, the EM_SETSEL message is ignored. In other words, the SendMessage() function passes the EM_SETSEL message too early to the edit control for it to become effective.
RESOLUTIONThere are two solutions to the above problem.
Solution 1Use SetFocus() to set the input focus on the edit control. Use PostMessage() to post the EM_SETSEL message to the edit control rather than using SendMessage() and return FALSE from the WM_INITDIALOG handler.
Solution 2When a newly created dialog box is displayed with focus on an edit control, the default text of the edit control is shown highlighted. In some cases, the text highlighting is undesirable because accidentally pressing a character key removes the original text from the edit control. Therefore, the workaround is to unselect the text by sending an EM_SETSEL message to the edit control at the dialog box initialization. Delay the EM_SETSEL message until the focus is set to the edit control. That is, while processing the first EN_SETFOCUS notification message, an EM_SETSEL message must be sent to the edit control to remove the highlight from its text. For example:
static BOOL bFirstTime; // We want to unselect only once. switch ( message ) { case WM_INITDIALOG: bFirstTime = TRUE; return TRUE; case WM_COMMAND: switch ( wParam ) { case IDC_EDIT: // If this is the first time, then unselect. if ( HIWORD( lParam ) == EN_SETFOCUS && bFirstTime ) { SendMessage(GetDlgItem( hwndDialog, IDC_EDIT ), EM_SETSEL, 0, MAKELPARAM( -1, -1 )); bFirstTime = FALSE; } break; . . . } // switch ( wParam ) . . . } // switch ( message ) |
Additional reference words: 3.10 3.50 3.51 4.00 95
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |