C# Winform button transparent background with properties and code

Lionsure 2020-07-15 Original by the website

Each control has a default background color(ControlLight) in Winform, and the button control also has this background color by default. If you don't want to change the beautiful background for the control, just use this default color.

The software is too simple and unattractive without appearance modification. The controls(such as Panel) that are generally used for layout must be added with a beautiful background, and the buttons need to be set with a transparent background to integrate into the background to form a complete and beautiful screen. Next, let's look at the setting method of button background transparency.

 

Setting method of C# Winform button transparent background

(1) Set with properties

1. Select the Button control and move the mouse to the "Properties" on the right to expand it, as shown in Figure 1:

 C# Winform button transparent background

Figure 1

If there is no "Properties" on the right, you can select the "View" menu → Properties window(Or right-click Button, select "Properties" in the pop-up menu), which will open the same window as Figure 1.

2. Click the drop-down box to the right of BackColor, select the "Web" tab, and then select Transparent as shown in Figure 1.

 

3. After setting the button background to transparent, it is still embossed, and then setting its style to Flat or Popup will no longer display the default style, that is, it is no longer possible to see if it is a button.

4. Find the FlatStyle property and set it to Flat or Popup, as shown in Figure 2:

Find the FlatStyle property and set it to Flat or Popup

Figure 2

The button is transparent, as shown in Figure 3:

The button is transparent

Figure 3

 

(2) Set with code

To set Button transparent with code, you need to set the style to Flat or Popup. The border of Button can be set to 0. Its MouseDownBackColor and MouseOverBackColor can also be set to transparent. The code is as follows:

public void ButtonTransparent(Button btn)
       {
              btn.FlatStyle = FlatStyle.Flat;
              btn.BackColor = Color.Transparent;
              btn.FlatAppearance.BorderSize = 0;
              btn.FlatAppearance.MouseDownBackColor = Color.Transparent;
              btn.FlatAppearance.MouseOverBackColor = Color.Transparent;
       }

Call: ButtonTransparent(button1);

 

(3) Set all Button backgrounds to transparent

Just use a loop to find all Buttons and set them with the ButtonTransparent method above. The code is as follows:

public void AllButtonsTransparent()
       {
              foreach (Control ctrl in this.Controls)
              {
                     if (ctrl is Button)
                     {
                            ButtonTransparent((Button)ctrl);
                     }
              }
       }

Call: AllButtonsTransparent();