Control.OnTextChanged(EventArgs) Metodo

Definizione

Genera l'evento TextChanged.

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)

Parametri

e
EventArgs

Oggetto EventArgs contenente i dati dell'evento.

Esempio

Nell'esempio di codice seguente viene modificato l'oggetto ForeColor di una TextBox classe derivata che visualizza i dati di valuta. Nell'esempio il testo viene convertito in un numero decimale e viene modificato in ForeColorColor.Red se il numero è negativo e in Color.Black se il numero è positivo. In questo esempio è necessario disporre di una classe che deriva dalla 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

Commenti

La generazione di un evento richiama il gestore eventi tramite un delegato. Per altre informazioni, vedere Gestione e generazione di eventi.

Il OnTextChanged metodo consente inoltre alle classi derivate di gestire l'evento senza associare un delegato. Questa è la tecnica preferita per gestire l'evento in una classe derivata.

Note per gli eredi

Quando si esegue l'override OnTextChanged(EventArgs) in una classe derivata, assicurarsi di chiamare il metodo della OnTextChanged(EventArgs) classe di base in modo che i delegati registrati ricevano l'evento.

Si applica a

Vedi anche