Setting MaxLength for TListView’s Edit Control (Plus: Forcing Edit Mode Using a Keyboard Shortcut)

TListView Edit Item CaptionThe TListView Delphi control displays a list of items in a fashion similar to how Windows File Explorer displays files and folders.

The ReadOnly property of a List View, determines whether the user can modify the caption of an item. When ReadOnly is false, the OnEditing and OnEdited events occur when the user begins and finishes editing the list item, respectively.

At run time the user can click the selected item to enter the edit mode. Actually, two clicks (not double click) are needed: a click on an item to select it and then a second click to go to edit mode.

Here’s how to initiate the edit mode by a keyboards shortcut and also how to limit the number of characters for the TListView’s editor control.

ListItem’s Begin_Edit Keyboard Shortcut: F2

If you want to allow the user to start editing the caption of an item by a keyboard “shortcut”, rather than by the mouse action (as described above), you need to handle the OnKeyDown event as:

Note: ListView1 on Form1, handling the OnKeyDown.

procedure TForm1.ListView1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState) ;
begin
  //start editing on F2 key press
  if Key = VK_F2 then
    if Assigned(ListView1.Selected) then
    ListView1.Selected.EditCaption;
end;

Upon entering the edit mode, the in-place editor for the item’s caption is placed “above” the selected item.

MaxLength For The ListView Editor Control

Upon entering the edit mode, the OnEditing event will get fired. Write an OnEditing event handler to take specific action just before a user edits a list item’s Caption property.

The edit control that is used by ListView to edit the caption of an item does not have the limit of the text you enter in it.

Since using TEdit we (Delphi developers) are used to have the MaxLength property. The MaxLength property you can find in TCustomEdit descendant controls specifies the maximum number of characters the user can enter into the edit control.

Therefore, here’s how to limit the number of characters a user can enter into the ListView’s editor control (for the Items’ Caption).

The ListView_GetEditControl function (located in “CommCtrl.pas” unit) retrieves the handle to the edit control being used to edit a list view item’s caption.

Once we have the handle to the edit control, we send it the EM_LIMITTEXT Windows message. This message limits the amount of text that a user can enter into an edit control.

Finally, we place the required code in the OnEditing event handler:

//handles OnEditing event of the "lvViewerFlags" ListView control
procedure TViewerKonfigUIForm.lvViewerFlagsEditing(Sender: TObject; Item: TListItem; var AllowEdit: Boolean);
var
  editorHandle : THandle;
begin
  editorHandle := ListView_GetEditControl(TListView(Sender).Handle);
  //max 12 characters
  SendMessage(editorHandle, EM_LIMITTEXT, 12, 0);
end;

The EM_LIMITTEXT is a Windows message you can send to an edit control to set its text limit – maximum amount of characters that the user can type into the edit control.

Note that you can use the OnEdited event to take specific action when a user has finished editing the text of a list item.

Let’s say you do not allow empty caption values, but rather you set a default caption for an item:

//OnEdited for lvViewerFlags ListView
procedure TViewerKonfigUIForm.lvViewerFlagsEdited(Sender: TObject; Item: TListItem; var S: string);
begin
  if Trim(s) = '' then s := 'No empty captions!';
end;

That’s it. Quick and simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.