RadioButton in C#

Introduction

A radio button is used when we select any one of the many options. It looks like a circular interface. Radio buttons are usually placed in a group on a container control, such as a panel or a group box, and the user can choose any one of these options.

Creating a RadioButton

We can create a RadioButton control using a Forms designer or runtime using the RadioButton class.

Forms designer

Let's create a RadioButton in forms designer, at first you create a windows form, then goto the toolbox = > drag the RadioButton and drop on the form.

creating a RadioButton

Runtime

Let's create a RadioButton in Runtime,then you use the RadioButton class.

RadioButton rb = new RadioButton();
rb.Text = "First RadioButton";
rb.Location = new Point(177, 100);
rb.Size = new Size(85, 17);
rb.AutoSize = true;
this.Controls.Add(rb);
creating runtime RadioButton

Properties of RadioButton

We will learn about all the properties of the radio button and use them.

  • BackColor

    The BackColor is used to change the background color of the radio button. It can be used both run time and compile time.

    BackColor of RadioButton
    radioButton1.BackColor = Color.Red;
    
  • CheckAlign

    The CheckAlign property is used to adjust the alignment of the circular interface of the radio button. It can be used both run time and compile time.

    CheckAlign of RadioButton
    radioButton1.CheckAlign = ContentAlignment.TopRight;
    
    Output of CheckAlign Property
     Output CheckAlign of RadioButton
  • Checked

    The checked property indicates whether the radio button has been checked or not. It can be used both run time and compile time.

    Checked RadioButton
    radioButton1.Checked = true;
    
    Output of Checked Property
     Output Checked of RadioButton
  • Appearance

    Appearance properties are used to set whether a raio button should look like a button or look like a normal radio button. But we can't use any round option if we use buttons. It can be used both run time and compile time.

    Appearance in  RadioButton
    radioButton1.Appearance = Appearance.Button;
    
    Output of Appearance Property
     Output Appearance of RadioButton
  • Font

    We will use the font property to change the text style of Radio Button set text. Inside the font property is the font family,font size,font style and so on. It can be used both run time and compile time.

    Font in  RadioButton
    radioButton1.Font = new Font("cursive",12F);
    
    Output of Font Property
     Output Font of RadioButton
  • ForeColor

    ForeColor is used to change the font color of RadioButton Text. It can be used both run time and compile time.

    ForeColor in  RadioButton
    radioButton1.ForeColor = Color.Red;
    
    Output of ForeColor Property
     Output Forecolor of RadioButton
  • Location

    Location property to change the location of a radio button. It can be used both run time and compile time.

    Location in  RadioButton
    radioButton1.Location = new Point(150, 100);
    
    Output of Location Property
     Output Location of RadioButton
  • Size

    Size property to change the size of a radio button. It can be used both run time and compile time.

    Size in  RadioButton
    radioButton1.Size = new Size(150, 150);
    
    Output of Size Property
     Output Size of RadioButton
  • Text

    Size property to change the size of a radio button. It can be used both run time and compile time.

    Text in  RadioButton
    radioButton1.Text = "Male";
    
    Output of Text Property
     Output Text of RadioButton
  • CheckedChanged

    CheckedChanged event to check the radio button checked are enabled or not. It can be used both run time and compile time.

    CheckedChanged in  RadioButton
    radioButton1.CheckedChanged+=new EventHandler(radioButton1_CheckedChanged);
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
    if (radioButton1.Checked)
    {
      //code here
    }
    else
    {
      //code here
    }
    }
    


Write a comment


Name Email Message