Control.ForeColor Control.ForeColor Control.ForeColor Control.ForeColor Property

定義

取得或設定控制項的前景色彩。Gets or sets the foreground color of the control.

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

屬性值

控制項的前景 ColorThe foreground Color of the control. 預設值為 DefaultForeColor 屬性的值。The default is the value of the DefaultForeColor property.

範例

下列程式碼範例會將BackColor控制項ForeColor的和設定為預設系統色彩。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一個子控制項的,但是子容器控制項( Panel例如或GroupBox)具有自己的子控制項,會更清楚地示範遞迴。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

備註

ForeColor屬性是環境屬性。The ForeColor 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.

給繼承者的注意事項

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

適用於

另請參閱