Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.

ListView.AfterLabelEdit événement


Date de publication : novembre 2016

Se produit lorsque l'utilisateur modifie l'étiquette d'un élément.

Espace de noms:   System.Windows.Forms
Assembly:  System.Windows.Forms (dans System.Windows.Forms.dll)

public event LabelEditEventHandler AfterLabelEdit

The E:System.Windows.Forms.ListView.AfterLabelEdit event occurs when the user finishes modifying the text for an item. The new string that the user types for the item is passed to the event, and the event handler can reject the change. If the event handler rejects the change, the text reverts to the text as it was before the user began editing the item.


Because the E:System.Windows.Forms.ListView.AfterLabelEdit event takes place before the label edit is committed, calling the M:System.Windows.Forms.ListView.Sort method in a handler for this event will sort the item using the original value.

In order for the E:System.Windows.Forms.ListView.AfterLabelEdit event to be raised, the P:System.Windows.Forms.ListView.LabelEdit property of the T:System.Windows.Forms.ListView control must be set to true.

You can create an event handler for the E:System.Windows.Forms.ListView.BeforeLabelEdit event to perform tasks before the user edits the text of an item.

For more information about handling events, see Handling and Raising Events.

The following code example demonstrates how to use the E:System.Windows.Forms.ListView.AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. The example uses the T:System.Text.ASCIIEncoding class to obtain the ASCII character code of each character of the new label. If the character falls between the ASCII codes that represent numbers, the new label cannot be applied to the item. This example requires that you have created a T:System.Windows.Forms.ListView control on a form and added items to it. The example also requires that you have connected the E:System.Windows.Forms.ListView.AfterLabelEdit event to the event handler defined in the example code. In order to use the T:System.Text.ASCIIEncoding class, your file must include the N:System.Text namespace.

private void MyListView_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)

   // Determine if label is changed by checking for null.
   if (e.Label == null)

   // 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.

.NET Framework
Disponible depuis 1.1
Retour au début