ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


5 - Fun With Groups

Once you have buttons displayed, grouping them naturally makes sense. In the case of check boxes, push buttons, and radio buttons, they remember their state, and there may be advantages to having them be mutually exclusive, with only one in a group down at a time. In the case of radio buttons, anything else is counterintuitive, since we are used to their exclusive response. However, as designed, these classes know nothing of exclusivity. To handle this, we can make use of the CButtonGroup class, which guarantees that only one button at a time is down in a group.

To make buttons behave this way, we do not add them directly to the form, but instead to the group; we then add the group to the form, like so:
  CButtonGroup *group=new CButtonGroup(0,65,53,true);
  // add buttons
  group->AddChild(new CRadioButton(100,50,15,"A-1"),13,5);
  group->AddChild(new CRadioButton(101,50,15,"A-2"),13,20);
  group->AddChild(new CRadioButton(102,50,15,"A-3"),13,35);
  // add group to form
  AddChild(group,125,5);
All these buttons are now managed, so as to prevent more than one from staying down. Note that the call for the CButtonGroup allows an optional border as the last parameter, so you can encircle the buttons visually or not, depending on your layout.

Since the button group is now maintaining these buttons, you must not change the button state directly with SetDownStatus(). Instead, the CButtonGroup calls are used: SetSelect() with the button index in the group, or SetSelectID() with the button id value, either of which turns on the selected button, and turns off the others in the group.

The demo program demonstrates how to set up a CButtonGroup, and shows you how your buttons too can be mutually exclusive.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice