Control.ForeColor 属性

定义

获取或设置控件的前景色。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.

示例

下面的代码示例将控件的 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

注解

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 将具有与其父 Form 相同的 BackColorFor 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. 不需要重写 ForeColor 属性的 getset 访问器;如果需要,只能重写一个。You are not required to override both the get and set accessors of the ForeColor property; you can override only one if needed.

适用于

另请参阅