Control.OnFontChanged メソッド

FontChanged イベントを発生させます。

Protected Overridable Sub OnFontChanged( _
   ByVal e As EventArgs _)
[C#]
protected virtual void OnFontChanged(EventArgse);
[C++]
protected: virtual void OnFontChanged(EventArgs* e);
[JScript]
protected function OnFontChanged(
   e : EventArgs);

パラメータ

  • e
    イベント データを格納している EventArgs

解説

イベントが発生すると、デリゲートを使用してイベント ハンドラが呼び出されます。詳細については、「 イベントの発生 」を参照してください。

OnFontChanged メソッドを使用すると、デリゲートを結び付けずに、派生クラスでイベントを処理することもできます。派生クラスでイベントを処理する場合は、この手法をお勧めします。

継承時の注意: 派生クラスで OnFontChanged をオーバーライドする場合は、登録されているデリゲートがイベントを受け取ることができるように、基本クラスの OnFontChanged メソッドを呼び出してください。

使用例

[Visual Basic, C#, C++] この例は、 Text プロパティの値が変更されたときに実行されるイベント生成メソッドです。 Control クラスには、 On PropertyName Changed という名前のパターンを持つメソッドがいくつかあります。これらは、PropertyName の値が変更されたときに、対応する PropertyName Changed イベントを発生させます。ここで、PropertyName は対応するプロパティの名前を表します。

[Visual Basic, C#, C++] 通貨型のデータを表示する TextBox の派生クラスの ForeColor を変更する例を次に示します。テキストを 10 進数に変換し、値が負の場合は ForeColorColor.Red に変更し、正の場合は Color.Black に変更する例を次に示します。この例では、 TextBox クラスから継承したクラスがあることを前提にしています。

 
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

[C#] 
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);
}

[C++] 
protected:
   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 (Exception*) {
         // If there is an error, display the
         // text using the system colors.
         this->ForeColor = SystemColors::ControlText;
      }

      TextBox::OnTextChanged(e);
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | FontChanged