Editing Labels

Lets the user of a ListView change the text of an item in place, in an edit box over it, and lets the program refuse or check the new text.

Private Sub ListView1_BeforeLabelEdit(Cancel As Boolean)
    If ListView1.SelectedItem.Key = "system" Then Cancel = True    ' this one keeps its name
End Sub

Private Sub ListView1_AfterLabelEdit(Cancel As Boolean, NewString As String)
    If Len(Trim$(NewString)) = 0 Then Cancel = True                ' no empty names
End Sub

Starting and ending an edit

With LabelEdit LvwLabelEditAutomatic, the user starts an edit by clicking the text of the item that has the focus, or by pressing F2. With LvwLabelEditManual only the program starts one, with StartLabelEdit, for example from a menu; with LvwLabelEditDisabled there is none. The edit box is hWndLabelEdit while it is open.

Every start raises BeforeLabelEdit, in which the program can refuse it. The user ends the edit with Enter, or by clicking elsewhere, and that raises AfterLabelEdit with the new text, in which the program can refuse the text: the item then keeps its old one. Esc ends the edit and leaves the text as it was, and so does EndLabelEdit; neither raises AfterLabelEdit.

Properties

hWndLabelEdit

The handle of the edit box while an item’s text is being edited, and 0 at other times. LongPtr, read-only.

IMEMode

The state of the Input Method Editor while the control, the edit box of a label or the filter box of a column has the focus. A member of CCIMEModeConstants. Default: CCIMEModeNoControl.

Another value raises run-time error 380.

LabelEdit

How an edit of an item’s text starts. A member of LvwLabelEditConstants. Default: LvwLabelEditAutomatic.

Another value raises run-time error 380.

Methods

EndLabelEdit

Ends the edit of an item’s text as the Esc key does: the item keeps the text it had.

Syntax: object.EndLabelEdit

StartLabelEdit

Opens the edit box over the item that has the focus, the SelectedItem, while LabelEdit is LvwLabelEditAutomatic or LvwLabelEditManual. The control must have the focus.

Syntax: object.StartLabelEdit

Events

AfterLabelEdit

Raised when the user ends an edit of an item’s text with Enter or by clicking elsewhere.

Syntax: object_AfterLabelEdit( Cancel As Boolean, NewString As String )

Cancel
False when the event is raised. A handler that sets it to True keeps the old text.
NewString
The text the user typed.

BeforeLabelEdit

Raised when an edit of the text of the SelectedItem starts, by the user or by StartLabelEdit.

Syntax: object_BeforeLabelEdit( Cancel As Boolean )

Cancel
False when the event is raised. A handler that sets it to True keeps the edit box closed.

LvwLabelEditConstants

The values of LabelEdit.

Member Value Description
LvwLabelEditAutomatic 0 The user starts an edit with a click on the text of the focused item, or with F2, and the program with StartLabelEdit.
LvwLabelEditManual 1 Only the program starts an edit, with StartLabelEdit.
LvwLabelEditDisabled 2 No edit starts.

See Also