Partage 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

Une EventArgs qui 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 valeur de propriété Text change. La classe Control a plusieurs méthodes avec le modèle de nom OnPropertyNameChanged qui déclenchent l’événement PropertyNameChanged correspondant lorsque la valeur PropertyName change (PropertyName représente le nom de la propriété correspondante).

L’exemple de code suivant modifie la ForeColor d’une classe dérivée TextBox affichant des données monétaires. L’exemple convertit le texte en nombre décimal et modifie l'ForeColor en Color.Red si le nombre est négatif et en Color.Black si le nombre est positif. Cet exemple nécessite que vous disposiez d’une classe qui dérive de la classe TextBox.

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 méthode OnLocationChanged 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 substitution de OnLocationChanged(EventArgs) dans une classe dérivée, veillez à appeler la méthode OnLocationChanged(EventArgs) de la classe de base afin que les délégués inscrits reçoivent l’événement.

S’applique à

Voir aussi