.Net Controls: RadioButton


 

Introduction

A radio button, sometimes called an option button, is circular control that comes in a group with other controls of the same type. Each radio button is made or an small empty circle O. From the group, when the user clicks one of them, the radio button that was clicked becomes filled with a big dot, like this 8. When one of the radio buttons in the group is selected and displays its dot, the others display empty circles. To guide the user as to what the radio buttons mean, each is accompanied by a label.

Radio Buttons Example

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Radio Buttons
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// This group box will be used to host the radio buttons
    	GroupBox *grpPizzaSizes;
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Selection of a Pizza Size";
    	
    	// Create a group box
    	grpPizzaSizes = new GroupBox;
    	grpPizzaSizes->Location = Point(8, 8);
    	grpPizzaSizes->Text = S"Select Desired Size";
    	grpPizzaSizes->Size = Drawing::Size(160, 110);
    	Controls->Add(grpPizzaSizes);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  12. Test the application

Creating Radio Buttons

To create a radio button, you can use the RadioButton class. Because radio buttons always come as a group, you should include them in another control that visibly shows that the radio buttons belong to the same group. The most common control used for this purpose is the group box created using the GroupBox control.

  1. To create radio buttons, you will declare three pointers to RadioButton class. Use the form's constructor to initialize the radio buttons. After creating each radio button, you should/must add it to the collection of controls managed by the Group Box control.
    Therefore, change the file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// This group box will be used to host the radio buttons
    	GroupBox *grpPizzaSizes;
    	RadioButton *rdoSmall;
    	RadioButton *rdoMedium;
    	RadioButton *rdoLarge;
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Selection of a Pizza Size";
    	
    	// Create a group box
    	grpPizzaSizes = new GroupBox;
    	grpPizzaSizes->Location = Point(8, 8);
    	grpPizzaSizes->Text = S"Select Desired Size";
    	grpPizzaSizes->Size = Drawing::Size(160, 110);
    	Controls->Add(grpPizzaSizes);
    
    	// Create the Small Size radio button
    	rdoSmall = new RadioButton;
    	rdoSmall->Location = Point(16, 24);
    	rdoSmall->Text = S"&Small";
    	// After creating a radio button, add it to the control
    	// that will host it.
    	// In this case, that will be the GroupBox control
    	grpPizzaSizes->Controls->Add(rdoSmall);
    
    	rdoMedium = new RadioButton;
    	rdoMedium->Location = Point(16, 48);
    	rdoMedium->Text = S"&Medium";
    	grpPizzaSizes->Controls->Add(rdoMedium);
    
    	rdoLarge = new RadioButton;
    	rdoLarge->Location = Point(16, 72);
    	rdoLarge->Text = S"&Large";
    	grpPizzaSizes->Controls->Add(rdoLarge);
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  2. Test the application

 

Radio Button Properties

Checked

While radio buttons come as a group, only one of them can be selected at a given time. The item that is selected has its Checked property set to true. There are two main ways you can use this property. To select a particular radio button, set its Checked property to true. To find out if an item is selected, get the value of its Checked property. You can also programmatically check a radio button. Here is an example:

private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
			 {
				 this->radioButton2->Checked = true;
			 }

 

The Check-Alignment

By default, the round box of a radio button control is positioned to the left side of its accompanying label. In Microsoft .Net applications, you have many options. Besides the left position, the most common alignment consists of positioning the round box to the right side of its label. The position of the round box with regards to its label is controlled by the CheckAlign property. The possible values are: TopLeft, TopCenter, TopRight, MiddleRight, BottomRight, BottomCenter, and BottomLeft. You can also do this programmatically as follows:

private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
		 {
			 this->radioButton1->CheckAlign = ContentAlignment::MiddleRight;
		 }

The Appearance

By default, radio buttons appear as rounded boxes that get filled with a big dot when the user selects one. Optionally, you can make a radio button appear as a toggle button. In that case, the buttons would appear as regular buttons. When the user clicks one, it appears down while the others are up. If the user clicks another button, the previous one becomes up while the new one would be down. To change the appearance of a radio button, assign the Button or Normal value to its Appearance property. The Appearance values are defined in the Appearance namespace. Here is an example:

private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
		 {
		     this->radioButton1->Appearance = Appearance::Button;
		 }

 

Radio Button Events

The most obvious, the most common, and the most regular event of a radio button just like any button is the OnClick event. This event fires when the user clicks the radio button.

  1. To implement the OnClick event of each radio button, change the file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc public class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// This group box will be used to host the radio buttons
    	GroupBox *grpPizzaSizes;
    	RadioButton *rdoSmall;
    	RadioButton *rdoMedium;
    	RadioButton *rdoLarge;
    	Label *lblSelected;
    
    	// These events will fire when each radio button is clicked
    	void SmallClicked(Object *Sender, EventArgs *Args);
    	void MediumClicked(Object *Sender, EventArgs *Args);
    	void LargeClicked(Object *Sender, EventArgs *Args);
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Selection of a Pizza Size";
    	
    	// Create a group box
    	grpPizzaSizes = new GroupBox;
    	grpPizzaSizes->Location = Point(8, 8);
    	grpPizzaSizes->Text = S"Select Desired Size";
    	grpPizzaSizes->Size = Drawing::Size(160, 110);
    	Controls->Add(grpPizzaSizes);
    
    	// Create the Small Size radio button
    	rdoSmall = new RadioButton;
    	rdoSmall->Location = Point(16, 24);
    	rdoSmall->Text = S"&Small";
    	rdoSmall->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoSmall->add_Click(new EventHandler(this, SmallClicked));
    	grpPizzaSizes->Controls->Add(rdoSmall);
    
    	rdoMedium = new RadioButton;
    	rdoMedium->Location = Point(16, 48);
    	rdoMedium->Text = S"&Medium";
    	rdoMedium->Checked = true;
    	rdoMedium->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoMedium->add_Click(new EventHandler(this, MediumClicked));
    	grpPizzaSizes->Controls->Add(rdoMedium);
    
    	rdoLarge = new RadioButton;
    	rdoLarge->Location = Point(16, 72);
    	rdoLarge->Text = S"&Large";
    	rdoLarge->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoLarge->add_Click(new EventHandler(this, LargeClicked));
    	grpPizzaSizes->Controls->Add(rdoLarge);
    
    	lblSelected = new Label;
    	lblSelected->AutoSize = true;
    	lblSelected->Location = Point(16, 130);
    	lblSelected->Text = S"Nothing Selected";
    	Controls->Add(lblSelected);
    }
    
    void SimpleForm::SmallClicked(Object *Sender, EventArgs *Args)
    {
    	lblSelected->Text = S"Small  $7.95";
    }
    
    void SimpleForm::MediumClicked(Object *Sender, EventArgs *Args)
    {
    	lblSelected->Text = S"Medium $10.75";
    }
    
    void SimpleForm::LargeClicked(Object *Sender, EventArgs *Args)
    {
    	lblSelected->Text = S"Large  $12.55";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *FM = new SimpleForm;
    	Application::Run(FM);
    
    	return 0;
    }
  2. Test the application:
     
    Radio Buttons Showing their Result

Related Examples:

Using Radio Buttons

Body Tag Formatter

 

Home Copyright © 2004-2014 FunctionX, Inc.