Skip to content

Latest commit

 

History

History
71 lines (59 loc) · 2.99 KB

how-to-set-the-background-of-a-windows-forms-panel.md

File metadata and controls

71 lines (59 loc) · 2.99 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Set the Background of a Panel
Learn how to set the background color and background image of a Windows Forms panel using the Designer.
03/30/2017
csharp
vb
cpp
background colors [Windows Forms], Windows Forms Panel controls
background images [Windows Forms], Windows Forms Panel controls
Panel control [Windows Forms], background
colors [Windows Forms], Windows Forms Panel controls
096cbd8d-45cc-47b8-b1ef-a27f60ea8be0

How to: Set the Background of a Windows Forms Panel

A Windows Forms xref:System.Windows.Forms.Panel control can display both a background color and a background image. The xref:System.Windows.Forms.Control.BackColor%2A property sets the background color for the contained controls, such as labels and radio buttons. If the xref:System.Windows.Forms.Control.BackgroundImage%2A property is not set, the xref:System.Windows.Forms.Control.BackColor%2A selection will fill the entire panel. If the xref:System.Windows.Forms.Control.BackgroundImage%2A property is set, the image will be displayed behind the contained controls.

To set the background programmatically

  1. Set the panel's xref:System.Windows.Forms.Control.BackColor%2A property to a value of type xref:System.Drawing.Color?displayProperty=nameWithType.

    Panel1.BackColor = Color.AliceBlue  
    panel1.BackColor = Color.AliceBlue;  
    panel1->BackColor = Color::AliceBlue;  
  2. Set the panel's xref:System.Windows.Forms.Control.BackgroundImage%2A property using the xref:System.Drawing.Image.FromFile%2A method of the xref:System.Drawing.Image?displayProperty=nameWithType class.

    ' You should replace the bolded image
    ' in the sample below with an image of your own choosing.  
    Panel1.BackgroundImage = Image.FromFile _  
        (System.Environment.GetFolderPath _  
        (System.Environment.SpecialFolder.Personal) _  
        & "\Image.gif")  
    // You should replace the bolded image
    // in the sample below with an image of your own choosing.  
    // Note the escape character used (@) when specifying the path.  
    panel1.BackgroundImage = Image.FromFile  
       (System.Environment.GetFolderPath  
       (System.Environment.SpecialFolder.Personal)  
       + @"\Image.gif");  
    // You should replace the bolded image
    // in the sample below with an image of your own choosing.  
    panel1->BackgroundImage = Image::FromFile(String::Concat(  
       System::Environment::GetFolderPath  
       (System::Environment::SpecialFolder::Personal),  
       "\\Image.gif"));  

See also