Partager via


Control.OnLocationChanged(EventArgs) Méthode

Définition

Déclenche l’événement LocationChanged.

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

Paramètres

e
EventArgs

Qui EventArgs contient les données d’événement.

Exemples

L’exemple de code suivant est une méthode de déclenchement d’événements qui est exécutée lorsque la Text valeur de propriété change. La Control classe a plusieurs méthodes avec le nom OnpropertyNameChanged qui déclenche l’événement PropertyName correspondant lorsque la valeur PropertyName change (PropertyNameChanged représente le nom de la propriété correspondante).

L’exemple de TextBox code suivant modifie la ForeColor classe dérivée affichant des données monétaires. L’exemple convertit le texte en nombre décimal et change la ForeColor valeur si Color.Red le nombre est négatif et si Color.Black le nombre est positif. Cet exemple nécessite que vous disposiez d’une classe qui dérive de la 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

Remarques

Le déclenchement d’un événement appelle le gestionnaire d’événements par le biais d’un délégué. Pour plus d’informations, consultez Gestion et déclenchement d’événements.

La OnLocationChanged méthode permet également aux classes dérivées de gérer l’événement sans attacher de délégué. Il s’agit de la technique recommandée pour gérer l’événement dans une classe dérivée.

Notes pour les héritiers

En cas de OnLocationChanged(EventArgs) substitution dans une classe dérivée, veillez à appeler la méthode de la classe de OnLocationChanged(EventArgs) base afin que les délégués inscrits reçoivent l’événement.

S’applique à

Voir aussi