TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# RadioButton Use: Windows Forms

Use the RadioButton control in Windows Forms. Access the Checked property.
RadioButton allows distinct selection of several items. In each group of RadioButtons, only one can be checked or selected. Conceptually, this control implements an exclusive selection. CheckBoxes allow multiple selections at once.
Steps. To begin, you should make a new Windows Forms application in Visual Studio, and then you can add GroupBox (or Panel) controls to it. In these controls, you can nest RadioButton instances.

Tip: By using this nesting strategy, you can have multiple groups of RadioButtons on a single form.

Tip 2: You may want to add the CheckedChanged event handler on your RadioButtons.

Also: You can actually have all the RadioButtons point to the same CheckedChanged event handler using the event window in Visual Studio.

Example RadioButton program: C# using System; using System.Windows.Forms; namespace WindowsFormsApplication11 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { // Executed when any radio button is changed. // ... It is wired up to every single radio button. // Search for first radio button in GroupBox. string result1 = null; foreach (Control control in this.groupBox1.Controls) { if (control is RadioButton) { RadioButton radio = control as RadioButton; if (radio.Checked) { result1 = radio.Text; } } } // Search second GroupBox. string result2 = null; foreach (Control control in this.groupBox2.Controls) { if (control is RadioButton) { RadioButton radio = control as RadioButton; if (radio.Checked) { result2 = radio.Text; } } } this.Text = result1 + " " + result2; } } }
Notes, example. We wired up all six RadioButtons to the radioButton1_CheckedChanged event handler. Thus, whenever the user clicks or changes the selection on the form, the above method will be executed.

Info: The radioButton1_CheckedChanged event handler has logic to update the title bar text whenever the user changes the selection of RadioButtons.

And: We loop over each sub-control of groupBox1 and groupBox2. When we encounter a RadioButton, we access Checked and Text.

Finally: We set the title text of the Window to our findings. This is to show the program correctly works.

Groups. In Windows Forms, the RadioButton controls are grouped according to their enclosing control. To create multiple groups of RadioButtons, you would need to add containers such as Panel or GroupBox and then put RadioButtons inside.
RadioButton, CheckBox. So when should you use a RadioButton as opposed to a CheckBox? If only one option can be logically selected, then you should use a RadioButton. If more than one (or zero) options can be selected, you should use the CheckBox.

Note: The example screenshot is only correct if the user can only have one Pet and only have one Home.

Also: For animal lovers or people who are wealthy and own many homes, the dialog would not be sufficient.

Summary. The RadioButton provides a user interface for an exclusive selection. We looked at how to use event handlers on RadioButtons. We also saw how to imperatively search RadioButtons for the checked one.

And: We discussed, conceptually, the use of RadioButtons and also their grouping behavior.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf