August 4th, 2004

Never leave focus on a disabled control

One of the big no-no’s in dialog box management is disabling the control that has focus without first moving focus somewhere else. When you do this, the keyboard becomes dead to the dialog box, since disabled windows do not receive input. For users who don’t have a mouse (say, because they have physical limitations that confine them to the keyboard), this kills your dialog box.

(I’ve seen this happen even in Microsoft software. It’s very frustrating.)

Before you disable a control, check whether it has focus. If so, then move focus somewhere else before you disable it, so that the user isn’t left stranded.

If you don’t know which control focus should go to, you can always let the dialog manager decide. The WM_NEXTDLGCTL message once again comes to the rescue.

void DialogDisableWindow(HWND hdlg, HWND hwndControl)
{
  if (hwndControl == GetFocus()) {
    SendMessage(hdlg, WM_NEXTDLGCTL, 0, FALSE);
  }
  EnableWindow(hwndControl, FALSE);
}

(And of course you should never disable the last control on a dialog. That would leave the user completely stranded with no hope of escape!)

[This was supposed to go out yesterday but the autoblog tool had a bad day and forgot to post this. Sorry.]

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.