How to set the size of the Button in C#?
Last Updated :
17 Apr, 2023
A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In windows forms, you are allowed to set the size of the button automatically using the AutoSize Property of the button. It is provided by Button class which helps programmers to create a button whose size is increased or decrease according to the content it contains. If you want to resize the button automatically according to its contents, then set the value of this property to true. And if you want to resize the button manually, then set the value of this property to false. You can use this property in two different methods: 1. Design-Time: It is the easiest method to resize the button automatically. Using the following steps you will set the AutoSize property of the button:
- Step 1: Create a windows form as shown in the below image: Visual Studio -> File -> New -> Project -> WindowsFormApp

- Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You are allowed to place a Button control anywhere on the windows form according to your need.

- Step 3: After drag and drop you will go to the properties of the Button control to set the AutoSize property of the Button.
Output: 
2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the AutoSize property of the Button programmatically with the help of given syntax:
public override bool AutoSize { get; set; }
The value of this property is of System.Boolean type. Following steps are used to set the AutoSize property of the Button:
- Step 1: Create a button using the Button() constructor is provided by the Button class.
// Creating Button using Button class
Button MyButton = new Button();
- Step 2: After creating Button, set the AutoSize property of the Button provided by the Button class.
// Set the AutoSize property of the button
Mybutton.AutoSize = true;
- Step 3: And last add this button control to form using Add() method.
// Add this Button to form
this.Controls.Add(Mybutton);
CSharp
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 WindowsFormsApp8 {
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.AutoSize = true;
l.Text = "Do you want to submit this form?";
l.Location = new Point(222, 145);
// Adding this label to form
this.Controls.Add(l);
// Creating and setting the properties of Button
Button Mybutton = new Button();
Mybutton.Location = new Point(225, 198);
Mybutton.Text = "Submit";
Mybutton.AutoSize = true;
Mybutton.BackColor = Color.LightBlue;
Mybutton.Padding = new Padding(6);
// Adding this button to form
this.Controls.Add(Mybutton);
// Creating and setting the properties of Button
Button Mybutton1 = new Button();
Mybutton1.Location = new Point(438, 198);
Mybutton1.Text = "Cancel";
Mybutton1.AutoSize = false;
Mybutton1.BackColor = Color.LightPink;
Mybutton1.Padding = new Padding(6);
// Adding this button to form
this.Controls.Add(Mybutton1);
}
}
}
- Output: Before setting the AutoSize property of the cancel button to false the output is like this:
After setting the AutoSize property of the cancel button to false the output is like this: 
Similar Reads
How to set the Font of the Button in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set the font of the button by using Font property. It is provided by Button class which helps programmers in creating more interact
3 min read
How to set the Name of the Button in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set the name of your button by using Name property. You can use this property in two different methods: 1. Design-Time: It is the e
3 min read
How to set the Padding of the button in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set the padding in your button by using the Padding Property. It is provided by Button class and used to manage appropriate space b
3 min read
How to set the Margin of the Buttons in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set the space between two or more Button controls using Margin Property. You can use this property in two different methods: 1. Des
3 min read
How to set the Size of the Label in C#? In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the size of Label control using Size Property. You can set this property using two different methods: 1. Design-Time: It is the easiest m
2 min read
How to set the Location of the Button in C#? A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In windows form, you are allowed to set the location of the button in your forms by using Location Property. It is provided by Button class and it helps us to se
3 min read