RadioButton

Introduction

RadioButtons provide a way to display multiple mutually-exclusive options to the user. Clicking on one RadioButton unchecks all other RadioButtons in a group.

Code Example: Creating RadioButtons

The following code creates RadioButtons for selecting difficulty:

// Initialize
var stackPanel = new StackPanel();
stackPanel.AddToRoot();

var easyRadioButton = new RadioButton();
stackPanel.AddChild(easyRadioButton);
easyRadioButton.Text = "Easy";

var mediumRadioButton = new RadioButton();
stackPanel.AddChild(mediumRadioButton);
mediumRadioButton.Text = "Medium";

var hardRadioButton = new RadioButton();
stackPanel.AddChild(hardRadioButton);
hardRadioButton.Text = "Hard";

Try on XnaFiddle.NETarrow-up-right

Grouping by Container

RadioButtons automatically group themselves based on their container. If RadioButtons are added directly to a Screen or Component, then they all use the same group and will all be mutually exclusive.

Alternatively, RadioButton instances can be grouped into separate containers (such as StackPanels) to control their grouping. the following example shows two StackPanels, each with three RadioButton instances.

StackPanels grouping RadioButtons

At runtime, the RadioButtons in each StackPanel are mutually exclusive.

Each RadioButton is mutually exclusive with the other RadioButtons in the same column

The following code creates two independent RadioButton groups side by side, one for difficulty and one for game mode. Selecting a RadioButton in one group does not affect the other group.

Try on XnaFiddle.NETarrow-up-right

Checked and Unchecked Events

RadioButton inherits Checked and Unchecked events from ToggleButton. Checked fires when a RadioButton becomes selected; Unchecked fires when it is deselected because another RadioButton in the same group was chosen.

Try on XnaFiddle.NETarrow-up-right

Reading IsChecked

To determine which RadioButton is selected at any point, check the IsChecked property on each instance. IsChecked is true for the selected button and false for all others in the group.

Try on XnaFiddle.NETarrow-up-right

Last updated

Was this helpful?