Control.ForeColor 屬性

定義

取得或設定控制項的前景色彩。

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

屬性值

Color

控制項的前景 Color。 預設值為 DefaultForeColor 屬性的值。

範例

下列程式碼範例會將 控制項的 和 ForeColor 設定 BackColor 為預設系統色彩。 如果控制項有任何子控制項,程式碼會以遞迴方式呼叫本身。 此程式碼範例需要您具有 Form 至少一個子控制項的 ,不過,子容器控制項,例如 PanelGroupBox ,其本身的子控制項 () 更能示範遞迴。

   // 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 是環境屬性。 環境屬性是控制項屬性,如果未設定,則會從父控制項擷取。 例如, Button 預設會有與其父 Form 系相同的 BackColor 。 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或類別概 Control 觀。

給繼承者的注意事項

在衍生類別中覆 ForeColor 寫 屬性時,請使用基類的 ForeColor 屬性來擴充基底實作。 否則,您必須提供所有實作。 您不需要同時覆寫 get 屬性的 ForeColorset 存取子;您可以視需要只覆寫一個。

適用於

另請參閱