ListView.OnAfterLabelEdit(LabelEditEventArgs) Méthode

Définition

Déclenche l’événement AfterLabelEdit.

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 qui contient les données d’événement.

Exemples

L’exemple de code suivant montre comment utiliser l’événement AfterLabelEdit pour limiter une étiquette récemment modifiée aux caractères de l’alphabet. L’exemple utilise la ASCIIEncoding classe pour obtenir le code de caractère ASCII de chaque caractère de la nouvelle étiquette. Si le caractère se situe entre les codes ASCII qui représentent des nombres, la nouvelle étiquette ne peut pas être appliquée à l’élément. Cet exemple nécessite que vous ayez créé un ListView contrôle sur un formulaire et que vous y avez ajouté des éléments. L’exemple exige également que vous ayez connecté l’événement AfterLabelEdit au gestionnaire d’événements défini dans l’exemple de code. Pour utiliser la ASCIIEncoding classe, votre fichier doit inclure l’espace de System.Text noms.

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é. Pour plus d’informations, consultez gestion et déclenchement d’événements.

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

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.

S’applique à

Voir aussi