Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Button in C#

  • Last Updated : 21 May, 2021

A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. For example, if a user wants to exit from the current application so, he/she click the exit button which closes the application. It can be used to perform many actions like to submit, upload, download, etc. according to the requirement of your program. It can be available with different shape, size, color, etc. and you can reuse them in different applications. In .NET Framework, Button class is used to represent windows button control and it is inherited from ButtonBase class. It is defined under System.Windows.Forms namespace. 
In C# you can create a button on the windows form by using two different ways:
1. Design-Time: It is the easiest method to create a button. Use the below 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 properties of the Button. 
     

2. Run-Time: It is a little bit trickier than the above method. In this method, you can create your own Button using the Button class. 
 



  • 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 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 Add() method. 
     
// Add this Button to form
this.Controls.Add(Mybutton);
  • Example:
     

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 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);
    }
}
}
  • Output: 
     

 

Important Properties of Button

 

 

PropertyDescription
BackColorUsing BackColor property you can set the background color of the button.
BackgroundImageUsing BackgroundImage property you can set the background image on the button.
AutoEllipsisUsing AutoEllipsis property you can set a value which 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.
AutoSizeUsing AutoSize property you can set a value which shows whether the button resizes based on its contents.
EnabledUsing Enabled property you can set a value which shows whether the button can respond to user interaction.
EventsUsing Events property you can get the list of the event handlers that are applied on the given button.
FontUsing Font property you can set the font of the button.
FontHeightUsing FontHeight property you can set the height of the font.
ForeColorUsing ForeColor property you can set the foreground color of the button.
HeightUsing Height property you can set the height of the button.
ImageUsing Image property you can set the image on the button.
MarginUsing Margin property you can set the margin between controls.
NameUsing Name property you can set the name of the button.
PaddingUsing Padding property you can set the padding within the button.
VisibleUsing Visible property you can set a value which shows whether the button and all its child buttons are displayed.

 

Important Events on Button

 

EventDescription
ClickThis event occur when the button is clicked.
DoubleClickThis event occur when the user performs double click on the button.
EnterThis event occur when the control is entered.
KeyPressThis event occur when the character, or space, or backspace key is pressed while the control has focus.
LeaveThis event occur when the input focus leaves the control.
MouseClickThis event occur when you click the mouse pointer on the button.
MouseDoubleClickThis event occur when you double click the mouse pointer on the button.
MouseHoverThis event occur when the mouse pointer placed on the button.
MouseLeaveThis event occur when the mouse pointer leaves the button.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :