Tuesday, September 29, 2009

Very simple Virtual Treeview tutorial

Yeah... I know how you(beginner) feel when it comes to Virtual Treeview from Soft-Gems.net, it's to hard to use!.
Take a deep breath as I guide you from "nub to intermediate" in under 20 minutes, all you need is:
 - to have a delphi installed on your system(if running then close it)
 - get Virtual Treeview from this link, click on Virtual Treeview Windows distribution
 - install it
Now we're going to do this step by step, but first start delphi then go to File -> New -> VCL Forms application - Delphi, add a TVirtualStringTree on the form from the "Virtual Controls" tab. As you can see from object inspector you got "stuff" load of options don't modify anything yet! go to events and find an event named OnGetNodeDataSize double-click the combobox on it's right and copy-paste this code
procedure TForm1.VirtualStringTree1GetNodeDataSize(Sender: TBaseVirtualTree;
  var NodeDataSize: Integer);
begin
  NodeDataSize := sizeof(PString);
end;

with the above code we tell the tree the size of each item we will display in it.
Now drop a button on a form, double-click it and copy-paste this code
procedure TForm1.Button1Click(Sender: TObject);
var
  index: integer;
  data: PString;
begin
  VirtualStringTree1.BeginUpdate;
  for index := 0 to 1000 do begin
    data := VirtualStringTree1.GetNodeData(VirtualStringTree1.AddChild(nil));
    data^ := Format('node #%d', [index]);
  end;
  VirtualStringTree1.EndUpdate;
end;

at this point we got everything we need except the event which needs to know the string it should display, so in order to fix this we need to search on our VirtualStringTree1's events for an event named OnGetText, copy-paste this code
procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
  Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: string);
var
  data: PString;
begin
  data := Sender.GetNodeData(Node);
  CellText := data^;
end;

I didn't comment this code because if you want a bit more complicated example you can browse thorough soft-gems.net or download a demo application which I made specially for this tutorial, it is well commented and above all seeing is buliving.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)