RadioButton

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit
Exam 70-526 Preparation Guide: Implement value-setting controls on a Windows Form


Contents


RadioButton controls enable the user to select a single option from a group. When the user selects one radio button within a group, the other radio buttons clear automatically. With this is mind, a group consists of several RadioButton controls within a given container, such as a Form. To create multiple groups on one form, you can place each group in its own container, such as a GroupBox or Panel control.

RadioButton and CheckBox controls have a similar function: they offer choices a user can select or clear. The difference is that multiple CheckBox controls can be selected at the same time, but radio buttons are mutually exclusive.

If you open the options dialog from the tools menu, you can see some radio buttons being used.

Image:526-109.jpg


Inheritance hierarchy

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Windows.Forms.Control
        System.Windows.Forms.ButtonBase
          System.Windows.Forms.RadioButton


Useful properties

  • Appearance - Gets or sets the value that determines the appearance of a RadioButton control. Takes a value from the Appearance enumeration:
  • Button - The appearance of a Windows button.
  • Normal - The default appearance defined by the control class.
  • AutoCheck - Indicates whether the Checked value and the RadioButton's appearance are automatically changed when the RadioButton is clicked.
  • Checked - Indicates whether the RadioButton is checked.
  • FlatStyle - Indicates the flat style appearance of the radio button control. Takes a value from the FlatStyle enumeration:
  • Flat - The control appears flat until the mouse pointer moves over it, at which point it becomes highlighted.
  • Popup - A control appears flat until the mouse pointer moves over it, at which point it appears three-dimensional.
  • Standard - The control appears three-dimensional (default).
  • System - The appearance of the control is determined by the user's operating system.
  • Image - Indicates the image that is displayed on the radio button control.
  • ImageAlign - Indicates the alignment of the image on the radio button control. Takes a value from the ContentAlignment enumeration:
  • BottomCenter - Content is vertically aligned at the bottom, and horizontally aligned at the center.
  • BottomLeft - Content is vertically aligned at the bottom, and horizontally aligned on the left.
  • BottomRight - Content is vertically aligned at the bottom, and horizontally aligned on the right.
  • MiddleCenter - Content is vertically aligned in the middle, and horizontally aligned at the center (default).
  • MiddleLeft - Content is vertically aligned in the middle, and horizontally aligned on the left.
  • MiddleRight - Content is vertically aligned in the middle, and horizontally aligned on the right.
  • TopCenter - Content is vertically aligned at the top, and horizontally aligned at the center.
  • TopLeft - Content is vertically aligned at the top, and horizontally aligned on the left.
  • TopRight - Content is vertically aligned at the top, and horizontally aligned on the right.
  • Text - The text that appears next to the radio button control.


CheckedChanged Event

The crucial event behind the RadioButton is the CheckedChanged event.

By default, the AutoCheck property is set to true, meaning that the button automatically toggles its check mark and fires a CheckedChanged event. By setting the AutoCheck property to false, you would need to handle the Click event and do manual checking on the Checked property.


Adding two RadioButtons manually

You can add RadioButtons to a form at run time in the following manner.

public void AddRadioButtons()
{
  RadioButton rBut1 = new RadioButton();
  rBut1.Location = new Point(50, 50);
 
  rBut1.AutoCheck = true;
  rBut1.Text = "Checked";
  rBut1.Checked = true;
 
  rBut1.CheckedChanged += new EventHandler(rBut_CheckedChanged);
 
  Controls.Add(rBut1);
 
  RadioButton rBut2 = new RadioButton();
  rBut2.Location = new Point(50, 80);
 
  rBut2.AutoCheck = true;
  rBut2.Text = "UnChecked";
  rBut2.Checked = false;
 
  rBut2.CheckedChanged += new EventHandler(rBut_CheckedChanged);
 
  Controls.Add(rBut2);
}
 
private void rBut_CheckedChanged(object sender, System.EventArgs e)
{
  if (((RadioButton)sender).Checked == true)
    ((RadioButton)sender).Text = "Checked";
  else
    ((RadioButton)sender).Text = "UnChecked";
}

By casting the sender object back to a RadioButton, the properties of the radio button can be accessed or altered. This is done because the RadioButton has no other reference on the form.


MSDN references


Previous_Page_.gif Next_Page_.gif
© 2007-2008 Mike Kitchen

Today's Deals: Electronics

Personal tools