How to Add Radio Buttons to a C# Windows Form

Giving Users Multiple Choices in a C# Application

© Mark Alexander Bain

Apr 20, 2009
Add a C# Radio Button, Mark Alexander Bain
Many form based applications use radio buttons to give users a multiple choice, and the C# programmer can do just the same with just a few lines of code.

Anyone that has every used a Windows form will have used a radio button (even if they don’t know what they are). Radio buttons are normally shown in groups and give the user a multiple choice, for example to ask them which language they’re using. For the C# programmer radio buttons provide an effective way of controlling user choices and, fortunately, they are very easy to work with.

Defining Radio Buttons with C#

As always with C# programming the first step in creating an application is to state which libraries arerequited and then to create a new class which extends the basic C# Windows form class:

using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {

The next thing to do is to define the components required in the form. In this case a panel and three radio will be required:

private Panel langPanel = new Panel();
private RadioButton radBritish = new RadioButton();
private RadioButton radFrench = new RadioButton();
private RadioButton radSpanish = new RadioButton();

The panel is required in order to group the radio buttons together. Radio buttons with the same parent (in this case the panel) always act as a single unit so that as one button is clicked all others in the group are unchecked automatically. Radio buttons with different parents (for example in more that one panel) will not interact with each other.

The programmer can then define their new class’s main and constructor functions:

public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
setUpRadioButtons ();
}

The class’s constructor function calls another function (setUpRadioButtons) and it’s this one that sets up the radio buttons and the other components that are to be used on the form.

Setting Up Radio Buttons with C#

At this point the components have been created but are not yet connected to the new form. The programmer must:

  • State where the component is to be placed on the form
  • Add the component to the form

The programmer uses the “Location” property to define where the component should lie. In this example the panel has been placed in the top left of the form:

public void setUpRadioButtons () {
langPanel.Location = new Point(0,0);

The location has two coordinates in “x” and “y”. Increasing “x” will move a component to the right, andincreasing “y” will move it down the form. Another useful property is “AutoSize”. Setting “AutoSize” to true will mean that the height and width of a component will automatically vary according do its contents:

langPanel.AutoSize = true;

Radio buttons share many of the same properties as panels (such as “Location” and “AutoSize”):

radBritish.Location = new Point(0,0);
radBritish.AutoSize = true;

However, there are some additional properties for the programmer to use. The “Text” is used to place an appropriate message next to the radio button, and setting “Checked” to true for a radio button will make it the default one:

radBritish.Text = "English";
radBritish.Checked = true;

Each of the radio buttons is set in the same way (but only one should have “Checked” set to true):

radFrench.Location = new Point(radBritish.Width,0);
radFrench.AutoSize = true;
radFrench.Text = "French";
radSpanish.Location = new Point
(radBritish.Width + radFrench.Width,0);
radSpanish.AutoSize = true;
radSpanish.Text = "Spanish";
lblDay.Location = new Point(0,radBritish.Height);
lblDay.AutoSize = true;

And finally the programmer must add the components to the form:

langPanel.Controls.Add(radBritish);
langPanel.Controls.Add(radFrench);
langPanel.Controls.Add(radSpanish);
this.Controls.Add(langPanel);
}

If the form is compiled at this point then the application user will see three simple but effective radio buttons.

Summary

C# programmers can use radio buttons to provide user options. Radio buttons (like all form components) must have a parent however each separate group of radio buttons must have their own parent (for example different panels).

Radio buttons grouped together with a single parent will act as a single unit, meaning that if a if one of the buttons is selected then all others will be unselected. This enables a C# programmer to provide their users with a set of options.


The copyright of the article How to Add Radio Buttons to a C# Windows Form in C Programming is owned by Mark Alexander Bain. Permission to republish How to Add Radio Buttons to a C# Windows Form in print or online must be granted by the author in writing.


Add a C# Radio Button, Mark Alexander Bain
A C# Radio Button, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo