A Button is an essential part of an application, software, or webpage. .NET Framework, the Button class is used to represent Windows button control and it is inherited from the ButtonBase class. It is defined under System.Windows.Forms namespace.
It allows users to interact with applications. Clicking an exit button closes the app, for example. Buttons enable actions like submitting or downloading, varying in appearance and reusable across programs.
Ways to Create a Button in Windows Forms
There are mainly two ways to create a button in Windows forms which are mentioned below.
- Drag and drop (Design-Time)
- Custom Button (Run-Time)
Drag and drop (Design-Time)
This is the easiest way to create a button in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the button on the form in the designer and further we can change the appearance of the button using the properties. Follow these steps to create a button.
Step 1: Locate the project in Visual Studio. In this example, we are using the default project name, Form1. Open the form in the editor to modify it further.
In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.
Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.
Step 3. Now open the Common Controls section, then drag and drop a button onto the form where you want it to be placed.
Step 4. Now open the properties of the button press right-click on the button and it will open the properties solution explorer now we can change the button name to Click here.
Now we are changing the Text property from button1 to Click Here.
Step 5: We can change the appearance and the behaviour of the button using the properties.
Output:
Custom Button (Run-Time)
In this method we are going to modify the Form1.cs file and add custom code modification in C# to change the appearance of the button according to our requirements. Follow these step by step process.
Step 1: Create a button using the Button() constructor provided by the Button class.
// Creating Button using Button class
Button MyButton = new Button();
Step 2: After creating the Button, set the properties of the Button provided by the Button class.
// Set the location of the button
Mybutton.Location = new Point(225, 198);
// Set text inside the button
Mybutton.Text = "Submit";
// Set the AutoSize property of the button
Mybutton.AutoSize = true;
// Set the background color of the button
Mybutton.BackColor = Color.LightBlue;
// Set the padding of the button
Mybutton.Padding = new Padding(6);
// Set font of the text present in the button
Mybutton.Font = new Font("French Script MT", 18);
Step 3: And last add this button control to form using the Add() method.
// Add this Button to form
this.Controls.Add(Mybutton);
Step 4: Now double-click on the button in Design and it will open the Form cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file
Form1.cs file:
C#
namespace WinFormsApp1 {
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 project?";
l.Location = new Point(222, 145);
l.Font = new Font("French Script MT", 18);
// 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);
Mybutton.Font = new Font("French Script MT", 18);
// Adding this button to form
this.Controls.Add(Mybutton);
// Creating and setting the properties of Button
Button Mybutton1 = new Button();
Mybutton1.Location = new Point(360, 198);
Mybutton1.Text = "Cancel";
Mybutton1.AutoSize = true;
Mybutton1.BackColor = Color.LightPink;
Mybutton1.Padding = new Padding(6);
Mybutton1.Font = new Font("French Script MT", 18);
// Adding this button to form
this.Controls.Add(Mybutton1);
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Output:
Properties of Button
These are the important properties of the button
Property | Description |
---|
BackColor | Using the BackColor property you can set the background color of the button. |
---|
BackgroundImage | Using the BackgroundImage property you can set the background image on the button. |
---|
AutoEllipsis | Using the AutoEllipsis property you can set a value shows that whether the ellipsis character (…) appears at the right edge of the control which denotes that the button text extends beyond the specified length of the button. |
---|
AutoSize | Using AutoSize property you can set a value which shows whether the button resizes based on its contents. |
---|
Enabled | Using Enabled property you can set a value which shows whether the button can respond to user interaction. |
---|
Events | Using the Events property you can get the list of the event handlers that are applied on the given button. |
---|
Font | Using Font property you can set the font of the button. |
---|
FontHeight | Using FontHeight property you can set the height of the font. |
---|
ForeColor | Using the ForeColor property you can set the foreground color of the button. |
---|
Height | Using the Height property you can set the height of the button. |
---|
Image | Using Image property you can set the image on the button. |
---|
Margin | Using the Margin property you can set the margin between controls. |
---|
Name | Using Name property you can set the name of the button. |
---|
Padding | Using the Padding property you can set the padding within the button. |
---|
Visible | Using the Visible property you can set a value which shows whether the button and all its child buttons are displayed. |
---|
Events on Button
Event | Description |
---|
Click | This event occurs when the button is clicked. |
---|
DoubleClick | This event occurs when the user performs a double click on the button. |
---|
Enter | This event occurs when the control is entered. |
---|
KeyPress | This event occurs when the character, or space, or backspace key is pressed while the control has focus. |
---|
Leave | This event occur when the input focus leaves the control. |
---|
MouseClick | This event occurs when you click the mouse pointer on the button. |
---|
MouseDoubleClick | This event occurs when you double-click the mouse pointer on the button. |
---|
MouseHover | This event occurs when the mouse pointer is placed on the button. |
---|
MouseLeave | This event occur when the mouse pointer leaves the button. |
---|
Similar Reads
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 size 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 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 hel
3 min read
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
RadioButton in C#
In Windows Forms, RadioButton control is a Graphical User Interface (GUI) used to select a single option among the group of options. For example, select a gender from the given list, so choose only one option among three options like Male or Female or Others. When we select a radio button any previo
5 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
CheckBox in C#
The CheckBox control is part of Windows form. It is a graphical user interface (GUI) control that is either checked or unchecked and used to select single or multiple elements from the given list or it can provide us options like yes or no, true or false, etc. It can be displayed as an image text or
6 min read
How to set the Visibility 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 a value which represents the button and its child buttons are displayed by using the Visible Property. It is provided by Button
3 min read
Label in C#
In Windows Forms, Label control is used to display text on the form. It does not interact with user input or handle mouse or keyboard events. Labels are used to provide information to the user within the form such as description, message, or details. These are the key points about labels.Display Tex
5 min read
C# Tutorial
C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read