The user can choose one choice from a list of options by using a radio button code in C# option buttons, leaving all other radio buttons in the same group unchecked. With the help of this article, you can easily learn how to write radio button code in C#.
What is an example of a radio button?
A list of circular holes with either a dot (for selected) or white space (for unselected) may be presented on the screen as an illustration of how radio buttons are grouped in groups of two or more (for selected). Every radio button typically has a label next to it that describes the option it represents.
Height, width, and size of radio buttons
The Location attribute accepts a Point as an argument, which determines the beginning location of a RadioButton on a Form. You can also utilize the Left and Top attributes to describe the placement of control from the Form’s left top corner. The Size attribute determines the control’s size. Instead of the Size property, we may alternatively utilize the Width and Height properties. The code below configures the Location, Width, and Height attributes of a radio button control.
- Dynamic Radio Button. Location = new Point (20, 150); 2. dynamicRadioButton.Height = 40; 3. dynamicRadioButton.Width = 300; 4. dynamicRadioButton.Width = 300;
Background, Foreground, and Border Style of a Radio Button
The BackColor and ForeColor attributes are used to specify the background and foreground colors of a RadioButton. The Color Dialog appears when you click on these parameters in the Properties window.
You may also change the background and foreground colors during runtime. The code below sets the BackColor and ForeColor attributes.
- BackColor = Red; 2. dynamicRadioButton.ForeColor = Blue;
The current text of a RadioButton control is represented by the text property of a RadioButton. The Left, Center, or Right text alignments are represented by the TextAlign property. The next piece of code determines a RadioButton control’s size and sets the Text and TextAlign attributes.
- dynamicRadioButton.Text = “I am a Dynamic RadioButton”;
- TextAlign = ContentAlignment.MiddleCenter;
The text font of a RadioButton control is represented by the RadioButton Font Font attribute. Font name, size, and other font settings are visible if you click the Font property in the Properties box. The Font property is run-time set in the following line of code.
- Font = newFont(“Georgia”, 16);
Browse RadioButton Contents
Using the Text property is the simplest approach to reading a Radio Button controls content. The contents of a RadioButton are read from a string in the following line of code.
- stringRadioButtonContents = dynamicRadioButton.Text;
The appearance of the RadioButton
The RadioButton’s Appearance property may be used to change the look of a RadioButton to that of a Button or a RadioButton. There is no round choice option with the Button look. The following property transforms a radio button into a Button control.
- Appearance = Appearance.Button;
The following code snippet uses an image as the backdrop of a radio button.
- Image = Image.FromFile(@ “C:\Images\Dock.jpg”);
- ImageAlign = ContentAlignment.MiddleRight;
- FlatStyle = FlatStyle.Flat;
A common radio button control can have two states: checked and unchecked. When the radio button is checked, it has a checkmark on it, and when it is not checked, it is unchecked. To check or uncheck a radio button, we often use a mouse.
When a radio button is checked, the checked attribute is true.
- Checked = true;
The AutoCheck attribute indicates whether the Checked or CheckState values, as well as the look of the RadioButton, are altered automatically when the RadioButton is clicked. This attribute is set to true by default but can be adjusted to false.
- AutoCheck = false;
Checked RadioButton Event Handler
When the Checked property’s value changes, the CheckedChanged event is triggered. To add this event handler, navigate to the Events pane and double-click on the CheckedChanged events, as shown in Figure 6.
The code snippet below specifies and implements these events and associated event handlers. You may use this code to dynamically implement the CheckedChanged event.
- CheckedChanged += newSystem.EventHandler(RadioButtonCheckedChanged);
- private void RadioButtonCheckedChanged(object sender, EventArgs e)