Open In App

How to set the AutoSize of the 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. You are allowed to set the size of the CheckBox automatically using the AutoSize property of the CheckBox. 
The value of this property is of System.Boolean type means it takes true if you want to resize the CheckBox according to the content, otherwise, false. The default value of this property is true. In Windows form, you can set this property in two different ways:
1. Design-Time: It is the simplest way to set the AutoSize property of a CheckBox using the following steps:
 





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

public override bool AutoSize { get; set; }

Following steps are used to set the AutoSize property of the CheckBox:
 

// Creating checkbox
CheckBox Mycheckbox = new CheckBox();
// Set the AutoSize property of the CheckBox
Mycheckbox.AutoSize = true;
// Add this checkbox to form
this.Controls.Add(Mycheckbox);




using System;
using System.Collections.Generic;
using System.ComponentModel;
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 City:";
        l.AutoSize = true;
        l.Location = new Point(233, 111);
        l.Font = new Font("Bradley Hand ITC", 12);
 
        // Adding label 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 = "Kolkata";
        Mycheckbox.AutoSize = true;
        Mycheckbox.Font = new Font("Bradley Hand ITC", 12);
 
        // 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, 198);
        Mycheckbox1.Text = "Bhubaneswar";
        Mycheckbox1.AutoSize = true;
        Mycheckbox1.Font = new Font("Bradley Hand ITC", 12);
 
        // Adding checkbox to form
        this.Controls.Add(Mycheckbox1);
    }
}
}


Article Tags :
C#