Control.OnFontChanged(EventArgs) Control.OnFontChanged(EventArgs) Control.OnFontChanged(EventArgs) Control.OnFontChanged(EventArgs) Method

定义

引发 FontChanged 事件。Raises the FontChanged event.

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

参数

e
EventArgs EventArgs EventArgs 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类的一些方法具有名称模式On PropertyName Changed的引发相应PropertyName Changed事件时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).

下面的代码示例中更改ForeColorTextBox派生的类显示货币数据。The following code example changes the ForeColor of a TextBox derived class displaying currency data. 此示例将文本转换为十进制数字,并更改ForeColorColor.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. 此示例需要您有派生一个类TextBox类。This 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.

OnFontChanged 方法还允许派生类对事件进行处理而不必附加委托。The OnFontChanged method also allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

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

适用于

另请参阅