3

I would like to implement a search procedure to a VirtualStringTree and I would like to do it by comparing the search text with the text from node and not from the pointer (eg. Data^.Column0) because this is not always as String.

Please help me with a suggestion to get back the text from node as it is.

For a better understanding see bellow code (I adjust an example from Lazarus)

type
  PTreeData = ^TTreeData;
  TTreeData = record
    Column0: TDate; //Date
    Column1: Integer; //Integer
    Column2: String;
  end;

procedure TForm1.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var
  Data: PTreeData;
begin
  Data := VST.GetNodeData(Node);
  case Column of
    0: CellText := DateToStr(Data^.Column0); //2015-05-11 or 11-05-2015
    1: CellText := IntToStr(Data^.Column1) + ' days'; //22 days
    2: CellText := Data^.Column2;
  end;
end;
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
REALSOFO
  • 852
  • 9
  • 37
  • How are you planning to get data for nodes if you have them linked through the node data pointer ? If you are asking how to write a generic solution, then your question (most probably) narrows to how to search for simple types by text, and throw away the virtual tree and pointer stuff. The rest what would remain is *I'm having a collection of records (internally in virtual tree through the node pointers), in which I want to search any simple type field by text*. – TLama May 11 '15 at 07:10
  • If there is no way then I will add to the `record` `Column0AsText` which will be the `Data^.Column0AsText:= DateToStr(Data^.Column0)` and I will implement the search through the strings. – REALSOFO May 11 '15 at 07:13
  • I can't make sense of this. You already have code to return a textual representation of each column. – David Heffernan May 11 '15 at 07:19

1 Answers1

3

If you want to get a virtual tree view cell text, then you can use the Text property. That will internally trigger the OnGetText event and you'll be able to obtain the text just like you are returning it to display in the tree:

var
  S: string;
  Node: PVirtualNode;
  Column: TColumnIndex;
begin
  ...
  S := VirtualStringTree.Text[Node, Column];
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 1
    You're welcome! But, well, I'm not a big fan of this way. I would rather tend to make a record *sugar* method, sort of `ToString` with the field index parameter (into which you'd be passing column index). Your code in the `OnGetText` handler would then become `CellText := Data.ToString(Column);` (but it depends on what you prefer and which Delphi version you have). – TLama May 11 '15 at 08:21
  • 1
    The main point of the virtual paradigm is to keep a clean separation between presentation and data. The use of `Text` goes against that. So, as TLama says in his comment, enhance the type that represents the data so that you can keep that separation. – David Heffernan May 11 '15 at 08:28
  • I see your point @David Heffernan. Anyhow @TLama, in the `while-do` I will compare search text with `ToString` function result. My problem is not in `GetText`, is in the search procedure. – REALSOFO May 11 '15 at 11:31
  • 1
    That sounds like a topic for a different question. The idea we were trying to point out is that you should not process your data in the view (virtual tree) and ask that view for these data from a different place but rather *teach* your data to do it by themselves. It's like putting a book that you're just reading into a library every time you finish reading a few chapters instead of keeping it by hand, e.g. on a table. – TLama May 11 '15 at 11:42