Control.BackColor Control.BackColor Control.BackColor Control.BackColor Property

定義

取得或設定控制項的背景色彩。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

備註

BackColor屬性不支援透明的色彩,除非SupportsTransparentBackColor的值System.Windows.Forms.ControlStyles設定為trueThe 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做為其父系Form預設。For 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. 您不需要覆寫兩個getset存取子BackColor屬性; 您可以覆寫只能有一個如有需要。You are not required to override both the get and set accessors of the BackColor property; you can override only one if needed.

適用於

另請參閱