PRB:Unselecting Edit Control Text at Dialog Box Initialization

Last reviewed: November 2, 1995
Article ID: Q96674
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SYMPTOMS

To 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.

CAUSE

While 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.

RESOLUTION

There are two solutions to the above problem.

Solution 1

Use 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 2

When 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
KBCategory: kbui kbprb
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.