Recent

Author Topic: Virtual tree with checkbox header and column  (Read 3812 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
Virtual tree with checkbox header and column
« on: January 08, 2021, 10:40:37 pm »
Hello everyone,

I have a virtual tree with an ID column with checkboxes and I want to select/deselect ALL the IDs in the list by selecting/deselecting the checkbox in the header (see screenshot).

Does anyone know how I can get this done?

Thanks,

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

balazsszekely

  • Guest
Re: Virtual tree with checkbox header and column
« Reply #1 on: January 09, 2021, 09:16:40 am »
Hi JD,

Quote
I have a virtual tree with an ID column with checkboxes and I want to select/deselect ALL the IDs in the list by selecting/deselecting the checkbox in the header (see screenshot).

Does anyone know how I can get this done?

I suppose you meant check/uncheck all, insted of select/deselect. Please try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckUncheckNodes(const Checked: Boolean);
  2. var
  3.   Node: PVirtualNode;
  4. begin
  5.   VDT.BeginUpdate;
  6.   try
  7.     Node := VDT.GetFirst;
  8.     while Assigned(Node) do
  9.     begin
  10.       if Checked then
  11.         VDT.CheckState[Node] := csCheckedNormal
  12.       else
  13.         VDT.CheckState[Node] := csUncheckedNormal;
  14.       Node := VDT.GetNext(Node);
  15.     end;
  16.   finally
  17.     VDT.EndUpdate;
  18.   end;
  19. end;
  20.  
  21. procedure TForm1.CheckBox1Click(Sender: TObject);
  22. begin
  23.   CheckUncheckNodes(CheckBox1.Checked);
  24. end;

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual tree with checkbox header and column
« Reply #2 on: January 10, 2021, 12:14:21 am »
Hi there GetMem,

Thanks a lot for the code. However I needed to clarify something. In the image, the ID is header row in the second column in the VirtualTree. So I want to check/uncheck the checkbox in the header of the virtualtree and automatically check/uncheck all the rows in the virtualtree.

I hope my description is clearer.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual tree with checkbox header and column
« Reply #3 on: January 10, 2021, 10:43:23 pm »
I want to put GetMem's code in an eventhandler for checking the box in the header of the ID column.

I think it will work that way.

Any ideas how I can do that?

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

balazsszekely

  • Guest
Re: Virtual tree with checkbox header and column
« Reply #4 on: January 11, 2021, 06:15:01 am »
@JD

There is an OnHeaderClick event for VirtualStringTree, using the code from my previous post:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.VDTHeaderClick(Sender: TVTHeader; HitInfo: TVTHeaderHitInfo);
  2. begin
  3.   if (hhiOnCheckbox in HitInfo.HitPosition) and (HitInfo.Column = 1) then //for the second, "ID" column
  4.   begin
  5.     if Sender.Columns[HitInfo.Column].CheckState = csCheckedNormal then
  6.       CheckUncheckNodes(True)
  7.     else
  8.       CheckUncheckNodes(False);
  9.   end;
  10. end;
« Last Edit: January 11, 2021, 08:25:55 am by GetMem »

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual tree with checkbox header and column
« Reply #5 on: January 13, 2021, 10:58:17 pm »
@GetMem

Perfect!!! Thanks a million.  :D :D

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

 

TinyPortal © 2005-2018