Control.BackColor 屬性

定義

取得或設定控制項的背景色彩。Gets or sets the background color for the control.

public:
 virtual property System::Drawing::Color BackColor { System::Drawing::Color get(); void set(System::Drawing::Color value); };
public virtual System.Drawing.Color BackColor { get; set; }
member this.BackColor : System.Drawing.Color with get, set
Public Overridable Property BackColor As Color

屬性值

Color ,代表控制項的背景色彩。A Color that represents the background color of the control. 預設值為 DefaultBackColor 屬性的值。The default is the value of the DefaultBackColor property.

範例

下列程式碼範例會將控制項的 BackColorForeColor 設定為預設系統色彩。The following code example sets the BackColor and ForeColor of the controls to the default system colors. 如果控制項有任何子控制項,則程式碼會以遞迴方式呼叫本身。The code recursively calls itself if the control has any child controls. 這個程式碼範例要求您必須具有至少一個子控制項的 Form:不過,子容器控制項(例如 PanelGroupBox)與它自己的子控制項,會更清楚地示範遞迴。This code example requires that you have a Form with at least one child control; however, a child container control, like a Panel or GroupBox, with its own child control(s) would better demonstrate the recursion.

   // Reset all the controls to the user's default Control color.
private:
   void ResetAllControlsBackColor( Control^ control )
   {
      control->BackColor = SystemColors::Control;
      control->ForeColor = SystemColors::ControlText;
      if ( control->HasChildren )
      {
         // Recursively call this method for each child control.
         IEnumerator^ myEnum = control->Controls->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Control^ childControl = safe_cast<Control^>(myEnum->Current);
            ResetAllControlsBackColor( childControl );
         }
      }
   }
// Reset all the controls to the user's default Control color. 
private void ResetAllControlsBackColor(Control control)
{
   control.BackColor = SystemColors.Control;
   control.ForeColor = SystemColors.ControlText;
   if(control.HasChildren)
   {
      // Recursively call this method for each child control.
      foreach(Control childControl in control.Controls)
      {
         ResetAllControlsBackColor(childControl);
      }
   }
}
' Reset all the controls to the user's default Control color. 
Private Sub ResetAllControlsBackColor(control As Control)
   control.BackColor = SystemColors.Control
   control.ForeColor = SystemColors.ControlText
   If control.HasChildren Then
      ' Recursively call this method for each child control.
      Dim childControl As Control
      For Each childControl In  control.Controls
         ResetAllControlsBackColor(childControl)
      Next childControl
   End If
End Sub

備註

除非 System.Windows.Forms.ControlStylesSupportsTransparentBackColor 值設定為 true,否則 BackColor 屬性不支援透明色彩。The BackColor property does not support transparent colors unless the SupportsTransparentBackColor value of System.Windows.Forms.ControlStyles is set to true.

BackColor 屬性是環境屬性。The BackColor property is an ambient property. 環境屬性是控制項屬性,如果未設定,則會從父控制項抓取。An ambient property is a control property that, if not set, is retrieved from the parent control. 例如,Button 預設會有相同的 BackColor 做為其父系 FormFor example, a Button will have the same BackColor as its parent Form by default. 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或 Control 類別的總覽。For more information about ambient properties, see the AmbientProperties class or the Control class overview.

給繼承者的注意事項

覆寫衍生類別中的 BackColor 屬性時,請使用基類的 BackColor 屬性來擴充基底的執行。When overriding the BackColor property in a derived class, use the base class's BackColor property to extend the base implementation. 否則,您必須提供所有的實作為。Otherwise, you must provide all the implementation. 您不需要覆寫 BackColor 屬性的 getset 存取子;您可以視需要覆寫其中一個。You are not required to override both the get and set accessors of the BackColor property; you can override only one if needed.

適用於

另請參閱