Feedback Search Top Backward Forward

Mnemonics in Dialog Boxes

Written by Stefan Ruck



[Here is a link to the source code. Ed.]

We all know them: Mnemonics (I'm glad I just have to write this word and not pronounce it). When we look at menu bars, we can find them almost anywhere. These nice underscored characters which make life easier for those who still use the keyboard instead of the mouse.

The Problem

Looking at dialog boxes e.g. of IBM's programs, I'm missing mnemonics a lot in conjunction with edit controls and other controls that don't have a text part of their own like radio buttons have.

Believe me, for a long time I thought it was not possible to use them in static text controls. Just because I hadn't seen it in the OS/2 programs I'm using! Of course I had a look at the docs shipped with the DevCon I CDs. But searching for the keyword 'Mnemonic' only shows some very old stuff about mnemonics in menu bars and something about the use of the Universal Resource Editor (which I'm not using).

So I was thinking about writing a class derived from IFrameWindow myself. This class should have the ability of putting mnemonics in static text controls to set the input focus directly e.g. to edit controls just by pressing the right keys, not by using the mouse.

But there is no need for such a class.

What's Needed?

Before I started writing this class, I thought to myself, why not try to do it the way I know it from Windows programming. There were only two things to think about.

First there was the question: How to get a character of a static text control to be underscored?

When you use IBM's dialog editor to design your dialogs, static text controls do not convert the '~' by default. And because I didn't knew the meaning of the word 'mnemonic' in the text style page I never tought of setting this flag. But a short look at the dictionary was very helpful. (When you use other controls like check boxes or radio button, there is no mnemonic flag to set to enable mnemonics. Here mnemonics always work.)

So the answer is simple. Just set the DT_MNEMONIC style for the desired control. Using the IBM's dialog editor you just have to check the mnemonic check box on the text style page of the control.

The second question is: How does the dialog know where to set the input focus when the mnemonic of a static text control is being used?

This is similiar to Windows programming (or vice versa). Just take care with the sequence of the dialog's controls (IBM's dialog editor calls it 'order groups', Microsoft's dialog editor calls it 'tab control'). The text control has to be directly in front of the corresponding control which should receive the input focus when the mnemonic is used. This doesn't mean the position in the dialog box but the sequence in the dialog description (have a look at the sample below).

As you can see, there is no miracle about mnemonics and static text controls. No need to do extra programming, just a little bit of dialog box design.

The Sample

Figure 1: Sample dialog box.

The dialog description for the dialog box shown in figure 1 looks like this:

    DIALOG  "Mnemonic Sample", DLG_SAMPLE, 8, 35, 141, 106,
      LTEXT           "~Edit Control", -1, 3, 89, 61, 8,
      ENTRYFIELD      "", DID_EDIT_CONTROL, 73, 89, 59, 8,
      LTEXT           "~Slider", -1, 3, 73, 61, 8,
      SLIDER          DID_SLIDER, 71, 67, 64, 16,
                      CTLDATA 12, 0, 20, 0, 0, 0
      GROUPBOX        "~Radiobutton Group", -1, 3, 24, 129, 40,
      AUTORADIOBUTTON "Radio 1", DID_RADIO_1, 7, 42, 60, 10,
                      WS_TABSTOP | WS_GROUP
      AUTORADIOBUTTON "Radio 2", DID_RADIO_2, 7, 28, 60, 10
      AUTORADIOBUTTON "Radio 3", DID_RADIO_3, 70, 42, 60, 10
      AUTORADIOBUTTON "Radio 4", DID_RADIO_4, 70, 28, 60, 10
      DEFPUSHBUTTON   "~OK", DID_OK, 44, 3, 40, 14, WS_GROUP

Figure 2: Dialog description of the sample dialog box.

As you can see, group boxes can also be used for mnemonics. And as said above, it is very important to keep the right sequence among the controls.

When 'Alt+E' is pressed in the dialog box shown in figure 1, the edit control receives the input focus, 'Alt+S' gives the slider the input focus and 'Alt+R' sets the input focus to the radio button selected the last time.

For test purpose, just swap the position of the text control 'Slider' and 'Edit Control' and see what happens. Because the sequence of the controls is very important, you can get funny (just for you, not for the user) effects by changing it.

I feel that using mnemonics in dialog boxes a little bit more than IBM does is a real help for anyone who is primary using the keyboard. It makes your programs more 'user friendly'. The user is able to work faster with your programs and so is more satisfied, and it costs you nearly nothing. So use them whenever possible.

In the source files there are two makefiles included. The one ending with .mav was made for IBM's VisualAge for C++. The one ending with .mac isn't a MAC file but the CSet++ makefile.