Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
AfterLabelEdit Event

ListView.AfterLabelEdit Event

Occurs when the label for an item is edited by the user.

[Visual Basic]
Public Event AfterLabelEdit As LabelEditEventHandler
[C#]
public event LabelEditEventHandler AfterLabelEdit;
[C++]
public: __event LabelEditEventHandler* AfterLabelEdit;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type LabelEditEventArgs containing data related to this event. The following LabelEditEventArgs properties provide information specific to this event.

Property Description
CancelEdit Gets or sets a value indicating whether changes made to the label of the ListViewItem should be canceled.
Item Gets the zero-based index of the ListViewItem containing the label to edit.
Label Gets the new text assigned to the label of the ListViewItem.

Remarks

The 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. In order for the AfterLabelEdit event to be raised, the LabelEdit property of the ListView control must be set to true. You can create an event handler for the BeforeLabelEdit event to perform tasks before the user edits the text of an item.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the AfterLabelEdit event to restrict a newly edited label to characters in the alphabet. The example uses the 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 assumes that you have created a ListView control on a form and added items to it. The example also assumes that you have connected the AfterLabelEdit event to the event handler defined in the example code. In order to use the ASCIIEncoding class, your file must include the System.Text namespace.

[Visual Basic] 
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 = 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

[C#] 
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;
      }
   }
}

[C++] 
private:
   void MyListView_AfterLabelEdit(Object* /*sender*/, System::Windows::Forms::LabelEditEventArgs* e) {

      // Determine if label is changed by checking for 0.
      if (e->Label == 0)
         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 (S"The text for the item cannot contain numerical values.");
            // Break out of the loop and exit.
            return;
         }
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ListView Class | ListView Members | System.Windows.Forms Namespace | OnAfterLabelEdit | BeforeLabelEdit | LabelEditEventHandler

Show:
© 2015 Microsoft