Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms. Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go. MFC has a very cool system for handling this, but the documentation is a bit unclear, and it helps to see the whole system in action.
I'll also show a useful technique of creating an enumeration variable to simplify identification and processing of the selected option. And I'll describe how to disable sub-items that are related to just one of the radio-button options to give your U/I that truly professional look.
In a "settings" dialog, you will typically need to present your user with several options related to a particular activity. When you have a largish number, say 4 or 5, then you will display a listbox or a combobox -- especially if that set of options might grow in future versions of your program.
But when there are only two or three options, it's usually best to show a set of Radio Buttons in your dialog (BTW... Never use a radio button when there is only one option -- use a checkbox instead).
MFC provides the tools, but the Wizard support is a bit finicky -- you need to do things in the right order if you want the Wizard to work for you, You can also add the radio-group handling manually, but it's worth knowing The MFC way.
- 1
- Use the Dialog Editor to design the form.
In the example, there are three radio buttons. There are some additional settings related to only the third option ("Custom").
- 2
- Set the Tab Order.
It's important that the radio buttons are numbered sequentially. To set the tab order, use the menu command
Format / Tab Order (Ctrl+D)
Click on each control in the order you want them. In particular, make sure that the radio buttons have sequential numbers.
- 3
- Set the Group attribute to True for the first button only.
Click on the other items to verify that "Group" is set to False.
- 4
- You need to add a DDX_Radio line to the DoDataExchange function. Let's let the Wizard do it for you.
In the Dialog Editor:
Right-click the top radio button and choose Add Variable... Set (in this order):
Category: Value
Variable type: int
Variable name: m_eRdoGrpFrab
Step 3 is key, because the "int" option is not available unless the "Group" attribute has been set!
My DoDataExchange function now looks like:
And when the dialog is closed, that variable will be set with the new, user-selected value (0, 1, or 2).
- "" title="An Additional Slightly Advanced (but very useful) Technique"]
...and change the DDX_Radio line in DoDataExchange() to:
Now you can use code like:
- "" title="About that Automatic Enabling..."]
In order to disable those other controls, do the following:
1) First, make sure the tab order is correct/sequential for the entire set.
2) Give a symbolic name (other than IDC_STATIC) to the first and last label.
3) Double-click each radio button to have the wizard provide an OnClick handler.
I usually collapse my code so that it looks like this:
I long-ago wrote a function that would let me easily disable (and enable) a sequential range of dialog controls. Here's that code, along with the code for DoEnabling:
And one last thing:
4) In your OnInitDialog function make a call to the DoEnabling() function.
Summary:
The MFC coders at Microsoft implemented support for automatically handling a group of radio buttons. This Article has described how to use that support quickly and easily. Knowing how the MFC Wizard works will make these U/I design and implementation tasks easier. And when you know the system, you can use more advanced techniques to make your forms more professional.
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-