Home > Back-end >  How to only checkmarking the selected item?
How to only checkmarking the selected item?

Time:07-24

as a beginner i know that code is used from FMX.ListViewCheckList Sample to put the checkmark accessory on the selected item:

  if AItem.Objects.AccessoryObject.Visible then
   begin
     AItem.Objects.AccessoryObject.Visible := False;
     FChecked.Remove(AItem.Index);
   end
   else
   begin
     AItem.Objects.AccessoryObject.Visible := True;
     FChecked.Add(AItem.Index)
   end;

But i want one selected item checmarked at a time so that If i check another item, the previous is unchecked. the code i did doesn't work:

procedure TMainForm.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
var
  i: integer;
begin
  if Assigned(ListView1.Selected) and (AItem.Objects.AccessoryObject.Visible) then
  begin
    AItem.Objects.AccessoryObject.Visible := False;
    FChecked.Add(AItem.Index);
  end else
  begin
    AItem.Objects.AccessoryObject.Visible := True;
    FChecked.Remove(AItem.Index);
  end;
end;

So cant you point me to the right direction How to do that ?

CodePudding user response:

I would simply save the checked item when it's checked, and uncheck it when another is checked.

CheckedItem:TListViewItem;  //Declared as a private field in your form.

procedure TMainForm.ListView1ItemClick(const Sender: TObject; const AItem: TListViewItem);
begin
if AItem.Objects.AccessoryObject.Visible then begin
     AItem.Objects.AccessoryObject.Visible := False;
     FChecked.Remove(AItem.Index);
     CheckedItem:=nil;
   end
   else begin
     if Assigned(CheckedItem) then begin   //If you've already checked an item, uncheck that one.
       CheckedItem.Objects.AccessoryObject.Visible := False;
       FChecked.Remove(CheckedItem.Index);
     end;
     AItem.Objects.AccessoryObject.Visible := True;
     FChecked.Add(AItem.Index);
     CheckedItem:=AItem;  //Save this one.
   end;
end;
  • Related