Search code examples
delphieditdelphi-6tlistview

How to disable TListView edit on item click?


I want to disable entering edit mode when I click on a TListView selected item but not disable it completely (setting property ReadOnly=True). I want to still be able to edit it by other methods. It is possible ?


Solution

  • I don't see any easy way to detect precisely how the LVN_BEGINLABELEDIT notification arises. And it's the LVN_BEGINLABELEDIT notification that triggers in-place edit of the list view.

    So, I think you'll probably need to come up with a slightly hacky solution. Add a Boolean field to your form, named FCanEditListView for example. Then wherever you trigger edit mode, set this flag True just before you trigger edit mode, and then revert it to False afterwards:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Item: TListItem;
    begin
      Item := ListView1.Selected;
      if Assigned(Item) then
      begin
        FCanEditListView := True;
        Item.EditCaption;
        FCanEditListView := False;
      end;
    end;
    

    Then add a handler for the OnEditing event of the list view, to switch behaviour as so:

    procedure TForm1.ListView1Editing(Sender: TObject; Item: TListItem;
      var AllowEdit: Boolean);
    begin
      AllowEdit := FCanEditListView;
    end;