Control.OnParentForeColorChanged(EventArgs) 方法

定义

当控件容器的 ForeColorChanged 属性值更改时,将引发 ForeColor 事件。Raises the ForeColorChanged event when the ForeColor property value of the control's container changes.

protected:
 virtual void OnParentForeColorChanged(EventArgs ^ e);
protected virtual void OnParentForeColorChanged (EventArgs e);
abstract member OnParentForeColorChanged : EventArgs -> unit
override this.OnParentForeColorChanged : EventArgs -> unit
Protected Overridable Sub OnParentForeColorChanged (e As EventArgs)

参数

e
EventArgs

包含事件数据的 EventArgsAn EventArgs that contains the event data.

示例

下面的代码示例是一个事件引发方法,该方法在 Text 属性值更改时执行。The following code example is an event-raising method that is executed when the Text property value changes. Control类有几个具有 name 模式 On PropertyName Changed 的方法, PropertyName Changedpropertyname值更改 (propertyname表示相应属性) 的名称时,这些方法会引发相应的 PropertyName 事件。The Control class has several methods with the name pattern OnPropertyNameChanged that raise the corresponding PropertyNameChanged event when the PropertyName value changes (PropertyName represents the name of the corresponding property).

下面的代码示例更改了 ForeColor TextBox 显示货币数据的派生类的。The following code example changes the ForeColor of a TextBox derived class displaying currency data. 该示例将文本转换为十进制数字, ForeColor Color.Red 如果数字为负,则将更改为, Color.Black 如果数字为正,则更改为。The example converts the text to a decimal number and changes the ForeColor to Color.Red if the number is negative and to Color.Black if the number is positive. 此示例要求具有派生自类的类 TextBoxThis example requires that you have a class that derives from the TextBox class.

protected:
   virtual void OnTextChanged( System::EventArgs^ e ) override
   {
      try
      {
         // Convert the text to a Double and determine
         // if it is a negative number.
         if ( Double::Parse( this->Text ) < 0 )
         {
            // If the number is negative, display it in Red.
            this->ForeColor = Color::Red;
         }
         else
         {
            // If the number is not negative, display it in Black.
            this->ForeColor = Color::Black;
         }
      }
      catch ( Exception^ ) 
      {
         // If there is an error, display the
         // text using the system colors.
         this->ForeColor = SystemColors::ControlText;
      }

      TextBox::OnTextChanged( e );
   }
protected override void OnTextChanged(System.EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine
      // if it is a negative number.
      if(double.Parse(this.Text) < 0)
      {
         // If the number is negative, display it in Red.
         this.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         this.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the 
      // text using the system colors.
      this.ForeColor = SystemColors.ControlText;
   }
   
   base.OnTextChanged(e);
}
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
   Try
      ' Convert the text to a Double and determine
      ' if it is a negative number.
      If Double.Parse(Me.Text) < 0 Then
         ' If the number is negative, display it in Red.
         Me.ForeColor = Color.Red
      Else
         ' If the number is not negative, display it in Black.
         Me.ForeColor = Color.Black
      End If
   Catch
      ' If there is an error, display the
      ' text using the system colors.
      Me.ForeColor = SystemColors.ControlText
   End Try

   MyBase.OnTextChanged(e)
End Sub

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息,请参阅 处理和引发事件For more information, see Handling and Raising Events.

OnParentForeColorChanged方法还使派生类可以处理事件,而无需附加委托。The OnParentForeColorChanged method also enables derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

在派生类中重写 OnParentForeColorChanged(EventArgs) 时,一定要调用基类的 OnParentForeColorChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。When overriding OnParentForeColorChanged(EventArgs) in a derived class, be sure to call the base class's OnParentForeColorChanged(EventArgs) method so that registered delegates receive the event.

适用于

另请参阅