다음을 통해 공유


Control.OnForeColorChanged 메서드

ForeColorChanged 이벤트를 발생시킵니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Protected Overridable Sub OnForeColorChanged ( _
    e As EventArgs _
)
‘사용 방법
Dim e As EventArgs

Me.OnForeColorChanged(e)
protected virtual void OnForeColorChanged (
    EventArgs e
)
protected:
virtual void OnForeColorChanged (
    EventArgs^ e
)
protected void OnForeColorChanged (
    EventArgs e
)
protected function OnForeColorChanged (
    e : EventArgs
)

매개 변수

  • e
    이벤트 데이터가 들어 있는 EventArgs입니다.

설명

이벤트를 발생시키면 대리자를 통해 이벤트 처리기가 호출됩니다. 자세한 내용은 이벤트 발생시키기를 참조하십시오.

또한 OnForeColorChanged 메서드를 사용하면 파생 클래스가 대리자를 연결하지 않고도 이벤트를 처리할 수 있습니다. 이는 파생 클래스에서 이벤트를 처리하는 기본 방법입니다.

상속자 참고 사항 파생 클래스에서 OnForeColorChanged을 재정의하는 경우 등록된 대리자가 이벤트를 받도록 기본 클래스의 OnForeColorChanged 메서드를 호출해야 합니다.

예제

다음 코드 예제는 Text 속성 값이 변경될 때 실행되는 이벤트 발생 메서드입니다. Control 클래스에는 PropertyName 값이 변경될 때 해당 PropertyNameChanged 이벤트를 발생시키는 OnPropertyName Changed 이름 패턴의 여러 메서드가 있습니다. 여기서 PropertyName은 해당 속성의 이름을 나타냅니다.

다음 코드 예제에서는 통화 데이터가 표시되는 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
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:
   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 void OnTextChanged(System.EventArgs e)
{
    try {
        // Convert the text to a Double and determine
        // if it is a negative number.
        if (System.Double.Parse(this.get_Text()) < 0) {
            // If the number is negative, display it in Red.
            this.set_ForeColor(Color.get_Red());
        }
        else {
            // If the number is not negative, display it in Black.
            this.set_ForeColor(Color.get_Black());
        }
    }
    catch (System.Exception exp) {
        // If there is an error, display the 
        // text using the system colors.
        this.set_ForeColor(SystemColors.get_ControlText());
    }
    super.OnTextChanged(e);
} //OnTextChanged

플랫폼

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에서 지원

참고 항목

참조

Control 클래스
Control 멤버
System.Windows.Forms 네임스페이스
ForeColorChanged