Microsoft KB Archive/96674

{|
 * width="100%"|

PRB: Unselecting Edit Control Text at Dialog Box Initialization

 * }

Q96674

-

The information in this article applies to:


 * Microsoft Windows Software Development Kit (SDK) 3.1
 * Microsoft Win32 Application Programming Interface (API), included with:
 * Microsoft Windows NT Server versions 3.5, 3.51
 * Microsoft Windows NT Workstation versions 3.5, 3.51
 * Microsoft Windows 95

-

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. #ifdef WIN32 if ( HIWORD( lParam ) == EN_SETFOCUS &&                  #else                   if ( HIWORD( wParam ) == EN_SETFOCUS && #endif {                     SendMessage(GetDlgItem( hwndDialog, IDC_EDIT ),                                  EM_SETSEL, 0,                      MAKELPARAM( -1, -1 )); bFirstTime = FALSE; }                  break; .          .           .           }  // switch ( wParam ) .     .      .   } // switch ( message ) Additional query words:

Keywords :

Issue type : kbprb

Technology : kbAudDeveloper kbSDKSearch kbWin32sSearch kbWin32API kbWinSDKSearch