Never leave focus on a disabled control

Raymond Chen

Raymond

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

Raymond Chen
Raymond Chen

Follow Raymond   

0 comments

Comments are closed.