Mirror

Ensure that at least one item in a TListView is always selected (Views: 6)


Problem/Question/Abstract:

The problem is that if the user clicks on a TListView outside the items the current selected item is deselected. I want my TListView to always keep one item selected so this never happens. What is the easiest way to accomplish this?

Answer:

{ ... }
Listview1.HideSelection := False;
{ ... }

{ ... }
var
  FPrevItem: TListItem;
  { ... }

procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not Assigned(Listview1.Selected) then
    Listview1.Selected := FPrevItem;
end;

procedure TForm1.ListView1Change(Sender: TObject; Item: TListItem; Change:
  TItemChange);
begin
  FPrevItem := Item;
end;

<< Back to main page