ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) ListView.OnAfterLabelEdit(LabelEditEventArgs) Method

Définition

Déclenche l'événement AfterLabelEdit.Raises the AfterLabelEdit event.

protected:
 virtual void OnAfterLabelEdit(System::Windows::Forms::LabelEditEventArgs ^ e);
protected virtual void OnAfterLabelEdit (System.Windows.Forms.LabelEditEventArgs e);
abstract member OnAfterLabelEdit : System.Windows.Forms.LabelEditEventArgs -> unit
override this.OnAfterLabelEdit : System.Windows.Forms.LabelEditEventArgs -> unit
Protected Overridable Sub OnAfterLabelEdit (e As LabelEditEventArgs)

Paramètres

e
LabelEditEventArgs LabelEditEventArgs LabelEditEventArgs LabelEditEventArgs

LabelEditEventArgs qui contient les données d'événement.A LabelEditEventArgs that contains the event data.

Exemples

L’exemple de code suivant montre comment utiliser le AfterLabelEdit événement se limiter aux caractères de l’alphabet une étiquette qui vient d’être modifiée.The following code example demonstrates how to use the AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. L’exemple utilise la ASCIIEncoding classe pour obtenir le code de caractère ASCII de chaque caractère de la nouvelle étiquette.The example uses the ASCIIEncoding class to obtain the ASCII character code of each character of the new label. Si le caractère est compris entre les codes ASCII qui représentent des nombres, la nouvelle étiquette ne peut pas être appliquée à l’élément.If the character falls between the ASCII codes that represent numbers, the new label cannot be applied to the item. Cet exemple nécessite que vous avez créé un ListView contrôle sur un formulaire et ajouté des éléments.This example requires that you have created a ListView control on a form and added items to it. L’exemple suppose également que vous avez connecté le AfterLabelEdit événement au gestionnaire d’événements défini dans l’exemple de code.The example also requires that you have connected the AfterLabelEdit event to the event handler defined in the example code. Pour pouvoir utiliser le ASCIIEncoding classe, votre fichier doit inclure le System.Text espace de noms.In order to use the ASCIIEncoding class, your file must include the System.Text namespace.

private:
   void MyListView_AfterLabelEdit( Object^ /*sender*/, System::Windows::Forms::LabelEditEventArgs^ e )
   {
      // Determine if label is changed by checking for 0.
      if ( e->Label == nullptr )
               return;

      // ASCIIEncoding is used to determine if a number character has been entered.
      ASCIIEncoding^ AE = gcnew ASCIIEncoding;

      // Convert the new label to a character array.
      array<Char>^temp = e->Label->ToCharArray();

      // Check each character in the new label to determine if it is a number.
      for ( int x = 0; x < temp->Length; x++ )
      {
         // Encode the character from the character array to its ASCII code.
         array<Byte>^bc = AE->GetBytes( temp[ x ].ToString() );

         // Determine if the ASCII code is within the valid range of numerical values.
         if ( bc[ 0 ] > 47 && bc[ 0 ] < 58 )
         {
            // Cancel the event and return the lable to its original state.
            e->CancelEdit = true;

            // Display a MessageBox alerting the user that numbers are not allowed.
            MessageBox::Show( "The text for the item cannot contain numerical values." );

            // Break out of the loop and exit.
            return;
         }
      }
   }
private void MyListView_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
{
 
   // Determine if label is changed by checking for null.
   if (e.Label == null)
      return;

   // ASCIIEncoding is used to determine if a number character has been entered.
   ASCIIEncoding AE = new ASCIIEncoding();
   // Convert the new label to a character array.
   char[] temp = e.Label.ToCharArray();

   // Check each character in the new label to determine if it is a number.
   for(int x=0; x < temp.Length; x++)
   {
      // Encode the character from the character array to its ASCII code.
      byte[] bc = AE.GetBytes(temp[x].ToString());
   
      // Determine if the ASCII code is within the valid range of numerical values.
      if(bc[0] > 47 && bc[0] < 58)
      {
         // Cancel the event and return the lable to its original state.
         e.CancelEdit = true;
         // Display a MessageBox alerting the user that numbers are not allowed.
         MessageBox.Show ("The text for the item cannot contain numerical values.");
         // Break out of the loop and exit.
         return;
      }
   }
}
Private Sub MyListView_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs) Handles listView1.AfterLabelEdit

   ' Determine if label is changed by checking to see if it is equal to Nothing.
   If e.Label Is Nothing Then
      Return
   End If
   ' ASCIIEncoding is used to determine if a number character has been entered.
   Dim AE As New ASCIIEncoding()
   ' Convert the new label to a character array.
   Dim temp As Char() = e.Label.ToCharArray()

   ' Check each character in the new label to determine if it is a number.
   Dim x As Integer
   For x = 0 To temp.Length - 1
      ' Encode the character from the character array to its ASCII code.
      Dim bc As Byte() = AE.GetBytes(temp(x).ToString())

      ' Determine if the ASCII code is within the valid range of numerical values.
      If bc(0) > 47 And bc(0) < 58 Then
         ' Cancel the event and return the lable to its original state.
         e.CancelEdit = True
         ' Display a MessageBox alerting the user that numbers are not allowed.
         MessageBox.Show("The text for the item cannot contain numerical values.")
         ' Break out of the loop and exit.
         Return
      End If
   Next x
End Sub

Remarques

Le déclenchement d'un événement appelle le gestionnaire des événements par l'intermédiaire d'un délégué.Raising an event invokes the event handler through a delegate. Pour plus d’informations, consultez gestion et déclenchement d’événements.For more information, see Handling and Raising Events.

La méthode OnAfterLabelEdit permet également aux classes dérivées de gérer l'événement sans y attacher de délégué.The OnAfterLabelEdit method also allows derived classes to handle the event without attaching a delegate. Il s'agit de la méthode recommandée pour gérer l'événement dans une classe dérivée.This is the preferred technique for handling the event in a derived class.

Notes pour les héritiers

Lors de la substitution de OnAfterLabelEdit(LabelEditEventArgs) dans une classe dérivée, veillez à appeler la méthode OnAfterLabelEdit(LabelEditEventArgs) de la classe de base de manière à ce que les délégués inscrits reçoivent l'événement.When overriding OnAfterLabelEdit(LabelEditEventArgs) in a derived class, be sure to call the base class's OnAfterLabelEdit(LabelEditEventArgs) method so that registered delegates receive the event.

S’applique à

Voir aussi