VirtualTreeview Example for Lazarus
©¦ English (en) ©¦ espa?ol (es) ©¦ fran?ais (fr) ©¦
Here are few examples on how to use VirtualTreeview for Lazarus (tested on win32). These are mostly collected from the web written for Delphi, and from the tutorial/docs by Philipp Frenzel and Mike Lischke.
The tutorial/docs can be downloaded from http://www.soft-gems.net. Below someone would find only the quick way to use VirtualTreeview on Lazarus, not explanations. For explanations and lots of other functions/methods, get the official documents and the tutorial.
Contents [hide]
1 Basic Tree Listview With 3 Columns
2 Checkbox
3 Images
4 Font Colour
5 Adding A Combobox
6 If Cell Editing Can't Be Seen
7 External links
Basic Tree Listview With 3 Columns
1. Install the component. Run lazarus.
2. Drop a TVirtualStringTree component (under Virtual Controls tab).
3. Go to the Source Editor (press F12). Under Uses clause add a unit - named VirtualTrees (if it is not already there and this is not the VirtualStringTree). So it may look like:
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
VirtualStringTree, VirtualTrees;
4. Get to the Form Designer (press F12). Select the Virtual Tree Component. On the Object Inspector click on Name, type VST and press enter. Click on Header (expand it) -> Columns, click on the small button next to "0 items". Click 3 times on Add button to add 3 columns. Do not close this window.
5. On the Column Editing window now the 3rd column is selected. Get to the Object Inspector. Click on Options (expand it) -> set coAllowClick to False.
6. Click on Text. Type Column2.
7. Click on Width, type 100 and press enter.
8. Get to the Column Editing window, select 1st and 2nd column, and set their property as above (for Text field, use different names, ie Column0, Column1).
9. Close the Column Editing window. Select the Virtual Tree Component on form. On Object Inspector get to Header -> Options (expand). Set hoVisible to True.
10. Scroll down to Style set it to hsFlatButtons.
11. Scroll down to TreeOptions(expand) -> MiscOption(expand), set toEditable to True. Set toGridExtensions to True.
12. Scroll down to SelectionOptions(expand) -> set toExtendedFocus to True. Set toMultiSelect to True. On the Form Designer resize VST (Virtual Tree Component) to get view of all the columns, if needed.
13. Now to add 3 buttons on the form. Get these from the component palette - Standard Tab ("OK" Label).
14. Click on Button1, on Object Inspector change Caption to AddRoot. Click on Button2, change the caption to AddChild. Change the caption of Button3 to Delete.
15. Keep this here and get to the Source Editor (press F12). On Source Editor replace the line:
{$mode objfpc}{$H+} with {$MODE DELPHI}
16. Under the "implementation" paste following lines:
type
PTreeData = ^TTreeData;
TTreeData = record
Column0: String;
Column1: String;
Column2: String;
end;
17. Get to the Form Designer (press F12). Select VST. Go to Object Inspector, select Events tab, scroll down to onChange. Double click on the combobox. Paste the follwings:
procedure TForm1.VSTChange(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
VST.Refresh;
end;
18. Scroll to onFocusChanged. Double click and paste the follwings:
procedure TForm1.VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex);
begin
VST.Refresh;
end;
19. Scroll to onFreeNode. Double click and paste the follwings:
procedure TForm1.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
Data: PTreeData;
begin
Data := VST.GetNodeData(Node);
if Assigned(Data) then begin
Data^.Column0 := '';