Recent

Author Topic: [SOLVED] Virtual TreeView add/delete columns at runtime  (Read 6370 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
[SOLVED] Virtual TreeView add/delete columns at runtime
« on: January 21, 2018, 01:26:22 pm »
Hi there everyone,

I have a virtual treeview that I use as a grid and I want to add columns to it dynamically. The data structure is just a dynamic array of string and it is working very well for this purpose. This means that I can increase or decrease the columns in the virtual treeview at will.

The code snippet for column creation and deletion is shown below:

Code: Pascal  [Select][+][-]
  1.       //
  2.       if ACreateColumns then
  3.       begin
  4.         ATree.BeginUpdate;
  5.        
  6.         //intOldColumns := ATree.Header.Columns.Count;
  7.         //// Delete the existing columns
  8.         //for intGridCol := 0 to Pred(intOldColumns) do
  9.         //  ATree.Header.Columns.Delete(intGridCol);
  10.  
  11.         ATree.Header.Columns.Clear;
  12.         //
  13.         for intGridCol := 0 to Pred(intColMax) do
  14.         begin
  15.           ATree.Header.Columns.Add;
  16.           ATree.Header.Columns[intGridCol].Width := 120;
  17.           // Column titles - from a JSON array
  18.           ATree.Header.Columns[intGridCol].Text := UpCase(StringReplace(jsArrayRow.Objects[0].Names[intGridCol], '_', ' ', [rfReplaceAll]))
  19.         end;
  20.        
  21.         //
  22.         ATree.Header.Options := ATree.Header.Options + [{hoAutoResize,} hoVisible];
  23.         ATree.Header.Height   := 26;
  24.         //
  25.         ATree.Invalidate;
  26.         ATree.EndUpdate;
  27.       end;
  28.  

The code above works but the only problem is if I fill the virtual treeview with many columns, then clear it and fill the virtual treeview with fewer columns than before, I can still see the columns I had deleted earlier. It is as if the virtual treeview does not repaint/refresh itself properly.

Can anyone help me figure out what is wrong?

Cheers,

JD
« Last Edit: January 24, 2018, 11:30:12 am by 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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Virtual TreeView add/delete columns at runtime
« Reply #1 on: January 21, 2018, 07:23:31 pm »
have you tried to call invalidate after the endupdate?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual TreeView add/delete columns at runtime
« Reply #2 on: January 21, 2018, 08:50:02 pm »
Hi there taazz,

I tried it but it still yields the same result. See screenshots

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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Virtual TreeView add/delete columns at runtime
« Reply #3 on: January 21, 2018, 09:02:06 pm »
Set the position field?

Anyway, here is my runtime instantiation, which is basically an evolved version of the grid in the "advanced" demo:

Code: Pascal  [Select][+][-]
  1.   b:=objtype.create; // self descriptive class for vsteditor module, based on TXMLConfig nodes.
  2.   for i:=0 to length(b.fielddescr)-1 do
  3.     begin
  4.       col:=actievst.header.Columns.Add;
  5.       col.Style:=vsOwnerDraw;
  6.       col.Options:=col.Options;
  7.       if invisible in b.fielddescr[i].properties then
  8.         col.Options:=col.Options-[covisible];
  9.       if b.fielddescr[i].fieldtype=vtboolean then
  10.        col.margin:=10
  11.       else
  12.        col.margin:=0;
  13.       if b.fielddescr[i].color=tcolor(0) then
  14.         col.Color:=globalcolor // colortest[i mod (high(colortest)+1)];
  15.       else
  16.         col.Color:=b.fielddescr[i].color;
  17.       col.position:=i+1;
  18.       col.width:=b.fielddescr[i].ColWidth;
  19.       col.Text := b.fielddescr[i].description;
  20.     end;
  21.   b.free;
  22.   ActieVST.Align:=alclient;
  23.   actievst.RootNodeCount:=numberitems;
  24.  
  25.   actievst.Invalidate;
  26.   actievst.Visible:=true;

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual TreeView add/delete columns at runtime
« Reply #4 on: January 23, 2018, 04:07:01 pm »
Hi there marcov,

Thanks for the reply. It still does not work.  :( :( :(

Your code is very similar to mine
Code: [Select]
for each field description
   add a column
   set the column's properties
   set the column header text
invalidate

The problem must be with the way older columns are removed.

I've tried ATree.Header.Columns.Clear and ATree.Clear and then called ATree.Invalidate but it is not working as expected. I still get the same visual results in the attachments I sent earlier.

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 TreeView add/delete columns at runtime
« Reply #5 on: January 23, 2018, 05:35:05 pm »
It works fine here. Which version do you use? Please test attached project.

PS: If you still have issues upload a small demo.
« Last Edit: January 23, 2018, 06:01:37 pm by GetMem »

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Virtual TreeView add/delete columns at runtime
« Reply #6 on: January 24, 2018, 11:27:49 am »
My sincere apologies to everyone. I found the problem. I ran the program on my Linux Mint 18.3 laptop and there were no drawing problems at all. It was just as I wanted it.

I then went back to my Windows laptop. I use Windows 7 but I added a theme to make it look like Windows 10  :(

Once I reverted to native Windows 7 theme and controls on the laptop, everything was perfect. No black headers, no drawing problems, everything was fine. I have egg on my face.

Thanks to everyone that offered suggestions and/or code.

JD
« Last Edit: January 24, 2018, 12:07:00 pm by 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