How to set the padding of CheckBox in C#?

The CheckBox control is the part of windows form which is used to take input from the user. Or in other words, CheckBox control allows us to select single or multiple elements from the given list. In CheckBox, you are allowed to set the space between the content and the CheckBox boundaries using the Padding property of the CheckBox. In Windows form, you can set this property in two different ways:

1. Design-Time: It is the simplest way to set the Padding property of a CheckBox using the following steps:

  • Step 1: Create a windows form. As shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the CheckBox control from the ToolBox and drop it on the windows form. You can place CheckBox anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the CheckBox control to set the padding in the CheckBox by using the Padding property.



    Output:

2. Run-Time: It is a little bit trickier method than the previous one. In this method, you can set the Padding property of a CheckBox programmatically using the following syntax:

public System.Windows.Forms.Padding Padding { get; set; }

Here, Padding is used to represent the space between the content and the ChechBox boundaries. Following steps are used to set the Padding property of the CheckBox:

  • Step 1: Create a checkbox using the CheckBox() constructor provided by the CheckBox class.
    // Creating checkbox
    CheckBox Mycheckbox = new CheckBox();
    
  • Step 2: After creating CheckBox, set the Padding property of the CheckBox provided by the CheckBox class.
    // Set the Padding property of the CheckBox
    Mycheckbox.Padding = new Padding(3);
    
  • Step 3 : And last add this checkbox control to form using Add() method.
    // Add this checkbox to form
    this.Controls.Add(Mycheckbox);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp5 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the properties of label
            Label l = new Label();
            l.Text = "Select language:";
            l.Location = new Point(233, 111);
      
            // Adding lable to form
            this.Controls.Add(l);
      
            // Creating and setting the properties of CheckBox
            CheckBox Mycheckbox = new CheckBox();
            Mycheckbox.Height = 50;
            Mycheckbox.Width = 100;
            Mycheckbox.Location = new Point(229, 136);
            Mycheckbox.Text = "C#";
            Mycheckbox.Padding = new Padding(3);
            // Adding checkbox to form
            this.Controls.Add(Mycheckbox);
      
            // Creating and setting the properties of CheckBox
            CheckBox Mycheckbox1 = new CheckBox();
            Mycheckbox1.Height = 50;
            Mycheckbox1.Width = 100;
            Mycheckbox1.Location = new Point(230, 174);
            Mycheckbox1.Text = "Phython";
            Mycheckbox.Padding = new Padding(3);
      
            // Adding checkbox to form
            this.Controls.Add(Mycheckbox1);
        }
    }
    }

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.