Control.ForeColor 属性

获取或设置控件的前景色。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable Property ForeColor As Color
用法
Dim instance As Control
Dim value As Color

value = instance.ForeColor

instance.ForeColor = value
public virtual Color ForeColor { get; set; }
public:
virtual property Color ForeColor {
    Color get ();
    void set (Color value);
}
/** @property */
public Color get_ForeColor ()

/** @property */
public void set_ForeColor (Color value)
public function get ForeColor () : Color

public function set ForeColor (value : Color)

属性值

控件的前景 Color。默认为 DefaultForeColor 属性的值。

备注

ForeColor 属性为环境属性。环境属性是一种控件属性,如果不设置,就会从父控件中检索该属性。例如,默认情况下,Button 与其父级 Form 一样具有相同的 BackColor。有关环境属性的更多信息,请参见 AmbientProperties 类或 Control 类概述。

给继承者的说明 在派生类中重写 ForeColor 属性时,请使用基类的 ForeColor 属性来扩展基实现。否则,您必须提供所有实现。不需要同时重写 ForeColor 属性的 getset 访问器;如果需要,可以只重写其中一个访问器。

示例

下面的代码示例将该控件的 BackColorForeColor 设置为默认系统颜色。如果该控件具有任何子控件,该代码将以递归方式调用它自身。此代码示例要求您拥有至少带有一个子控件的 Form;不过,一个带有它自己的子控件的子容器控件(如 PanelGroupBox)可以更好地演示递归。

' 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 Me.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
// 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(this.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:
   void ResetAllControlsBackColor( Control^ control )
   {
      control->BackColor = SystemColors::Control;
      control->ForeColor = SystemColors::ControlText;
      if ( this->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.set_BackColor(SystemColors.get_Control());
    control.set_ForeColor(SystemColors.get_ControlText());
    if (this.get_HasChildren()) {
        for (int iCtr=0; iCtr < control.get_Controls().get_Count(); iCtr++) {
            // Recursively call this method for each child control.
            Control childControl = control.get_Controls().get_Item(iCtr);
            ResetAllControlsBackColor(childControl);
        }
    }
} //ResetAllControlsBackColor

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control 类
Control 成员
System.Windows.Forms 命名空间
ForeColorChanged
OnForeColorChanged
Color