Open In App

How to set the Background color of the Button in C#?

Improve
Improve
Like Article
Like
Save
Share
Report

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 background color of the button with the help of BackColor property. This property is provided by Button class and helps programmers to create more good looking buttons in their forms. You can use this property in two different methods: 1. Design-Time: It is the easiest method to set the background color of the button. Using the following steps you will set the background color of your 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 BackColor 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 BackColor property of the Button programmatically with the help of given syntax:

public override System.Drawing.Color BackColor { get; set; }

Here, Color indicates the background color of the button. Following steps are used to set the BackColor 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 BackColor property of the Button provided by the Button class.
// Set the BackColor property of the button
Mybutton.BackColor = Color.LightBlue;
  • 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 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;
 
        // 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 = true;
        Mybutton1.BackColor = Color.LightPink;
 
        // Adding this button to form
        this.Controls.Add(Mybutton1);
    }
}
}


  • Output:


Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads