{"id":111825,"date":"2025-12-01T07:00:00","date_gmt":"2025-12-01T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111825"},"modified":"2025-12-01T07:21:43","modified_gmt":"2025-12-01T15:21:43","slug":"20251201-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20251201-00\/?p=111825","title":{"rendered":"How do I get my edit control text to be autoselected when I choose it to be the default focus in my dialog?"},"content":{"rendered":"<p>A customer had a Win32 dialog with a bunch of edit controls inside. In their <code>WM_<wbr \/>INIT\u00adDIALOG<\/code> message handler, they filled those edit controls with information and then decided which one should get initial focus when the dialog is shown to the user. To set the focus, they did<\/p>\n<pre>SetFocus(GetDlgItem(hDlg, initialControlId));\r\n<\/pre>\n<p>and then they returned <code>FALSE<\/code> to indicate that they had already set the focus and the dialog manager should not attempt to set its own focus.<\/p>\n<p>This worked well, with the exception that the edit control they chose did not have its contents autoselected, like the dialog manager would have done if it were given permission to set the focus. How could they get that extra behavior?<\/p>\n<p>This is sort of the reverse of the problem of <a title=\"Preventing edit control text from being autoselected in a dialog box\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20031114-00\/?p=41823\"> preventing edit control text from being autoselected in a dialog box<\/a>. We solved that problem by removing the <code>DLGC_<wbr \/>HAS\u00adSET\u00adSEL<\/code> dialog code from the edit control. In this case, the edit control <i>has<\/i> the <code>DLGC_<wbr \/>HAS\u00adSET\u00adSEL<\/code> dialog code, but how do we set focus in a way that honors that code?<\/p>\n<p>We saw the answer some time ago: <a title=\"How to set focus in a dialog box\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040802-00\/?p=38283\"> How to set focus in a dialog box<\/a>. You can have the dialog send itself the <code>WM_<wbr \/>NEXT\u00adDLG\u00adCTL<\/code> message to ask it to set focus to the desired control in the standard dialog manner (which includes autoselecting text in edit controls) and updating the internal dialog box bookkeeping to keep track of things like the dialog default pushbutton, so that you don&#8217;t get into weird states like a dialog box with two default buttons.<\/p>\n<pre>SendMessage(hDlg, WM_NEXTDLGCTL,\r\n            (WPARAM)GetDlgItem(hDlg, initialControlId), TRUE);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remembering some old APIs.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-111825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Remembering some old APIs.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=111825"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111825\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=111825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}