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. Design-Time: It is the easiest method to set the margin between two button controls. 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 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 Margin property of the Button.
Output:

Run-Time: It is a little bit trickier than the above method. In this method, you can set the Margin property of the Button programmatically with the help of given syntax:
public System.Windows.Forms.Padding Margin { get; set; }
Here, the Padding is used to represent the space between controls. Following steps are used to set the Margin 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 Margin property of the Button provided by the Button class.
// Set the margin of the button Mybutton.Margin = new Padding(5, 5, 5, 5);
- Step 3: And last add this button control to form using Add() method.
// Add this Button to form this.Controls.Add(Mybutton);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp8 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the properties of labelLabel l =newLabel();l.AutoSize =true;l.Text ="Do you want to submit this form?";l.Location =newPoint(222, 145);l.Font =newFont("French Script MT", 18);// Adding this label to formthis.Controls.Add(l);// Creating and setting the properties of ButtonButton Mybutton =newButton();Mybutton.Location =newPoint(225, 198);Mybutton.Text ="Submit";Mybutton.AutoSize =true;Mybutton.BackColor = Color.LightBlue;Mybutton.Padding =newPadding(6);Mybutton.Font =newFont("French Script MT", 18);Mybutton.Margin =newPadding(5, 5, 5, 5);// Adding this button to formthis.Controls.Add(Mybutton);// Creating and setting the properties of ButtonButton Mybutton1 =newButton();Mybutton1.Location =newPoint(360, 198);Mybutton1.Text ="Cancel";Mybutton1.AutoSize =true;Mybutton1.BackColor = Color.LightPink;Mybutton1.Padding =newPadding(6);Mybutton1.Font =newFont("French Script MT", 18);Mybutton1.Margin =newPadding(5, 5, 5, 5);// Adding this button to formthis.Controls.Add(Mybutton1);}}}chevron_rightfilter_noneOutput:

Recommended Posts:
- How to set the Margin between the CheckBox controls in C#?
- How to set the Margin between the TextBox controls in C#?
- How to set Alignment of Up and Down buttons of NumericUpDown in C#?
- How to set the foreground color of the CheckBox in C#?
- Array.GetValue() Method in C# with Examples | Set - 1
- How to set the Visibility of the Button in C#?
- How to set the Alignment of the Text in RadioButton in C#?
- How to set Text in the RadioButton in C#?
- How to set the Size of the RadioButton in C#?
- How to set the Font of the ListBox in C#?
- How to set the Alignment of the Text in the TextBox in C#?
- C# | Check if two String objects have the same value | Set-1
- C# | Graphics.DrawLine() Method | Set - 1
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | Structures | Set - 1
- How to set the Location of the TextBox in C#?
- C# | String.IndexOf( ) Method | Set - 1
- C# | Join() Method | Set - 1
- C# | Join Method | Set - 2
- C# | Math.Round() Method | Set - 1
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.

