Control.OnTextChanged(EventArgs) Método

Definição

Eleva o TextChanged evento.

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

Parâmetros

e
EventArgs

E EventArgs que contém os dados do evento.

Exemplos

O seguinte exemplo de código altera o ForeColor de uma TextBox classe derivada que apresenta dados de moeda. O exemplo converte o texto num número decimal e altera o ForeColor para Color.Red se o número for negativo e para Color.Black se o número for positivo. Este exemplo exige que tenhas uma classe que derive dessa TextBox classe.

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

Observações

Levantar um evento invoca o gestor de eventos através de um delegado. Para mais informações, consulte Manuseio e Levantamento de Eventos.

O OnTextChanged método também permite que classes derivadas tratem do evento sem anexar um delegado. Esta é a técnica preferida para lidar com o evento numa classe derivada.

Notas para Herdeiros

Ao substituir OnTextChanged(EventArgs) uma classe derivada, certifique-se de chamar o método da OnTextChanged(EventArgs) classe base para que os delegados registados recebam o evento.

Aplica-se a

Ver também