This documentation is archived and is not being maintained.

ListView.BeforeLabelEdit Event

Occurs when the user starts editing the label of an item.

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

[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 BeforeLabelEdit event occurs when the user begins modifying the text for an item. If the event handler cancels this event, the user cannot edit the text. You can use this event to prevent the user from editing specific items in the ListView control. If the LabelEdit property of the ListView control is set to false, the BeforeLabelEdit event is not raised; all user attempts to edit item labels are automatically rejected.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#] The following code example demonstrates handling the ListView. BeforeLabelEdit event and using the LabelEditEventArgs class. To run the example, paste the following code in a form containing a ListView control named ListView1 and populated with at least 3 items.

[Visual Basic] 

Private Sub ListView1_BeforeLabelEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.LabelEditEventArgs) _
    Handles ListView1.BeforeLabelEdit

    ' Allow all but the first two items of the list to be modified by
    ' the user.
    If (e.Item < 2) Then
        e.CancelEdit = True
    End If
End Sub

[C#] 
private void ListView1_BeforeLabelEdit(object sender, 
 System.Windows.Forms.LabelEditEventArgs e)
    {
 // Allow all but the first two items of the list to 
 // be modified by the user.
 if (e.Item<2)
 {
     e.CancelEdit = true;
 }
    }

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic 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 | OnBeforeLabelEdit | AfterLabelEdit | LabelEditEventHandler

Show: