Control.ForeColor 属性


获取或设置控件的前景色。Gets or sets the foreground color of the control.

 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 至少具有一个子控件; 但是,子容器控件(如 PanelGroupBox )具有其自己的子控件 (s) 可以更好地演示递归。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.
   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;
      // Recursively call this method for each child control.
      foreach(Control childControl in control.Controls)
' 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
      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 属性的和 set 访问器 ForeColor ; 如果需要,只能重写一个。You are not required to override both the get and set accessors of the ForeColor property; you can override only one if needed.