As you pointed out in the comments, your difficulty is to assemble a Treeview, I’ll leave a solution with this focus.
For the solution I used:
- A Tmemo as a basis for the file.
- A Ttreeview.
- A Tvaluelisteditor to store the fields that will be outside the tree, invisible as it will only serve to store the temporary data.
- Other Tmemo to show the fields of the selected node in the tree.
Codes
Declare the type TVetorDeString
type
TVetorDeString = array of string;
Click on "Process"
var
i: Integer;
Linha: TVetorDeString;
NohNivel1: TTreeNode;
NohNivel2: TTreeNode;
begin
SetLength(Linha, 0);
for i := 0 to Memo1.Lines.Count-1 do
begin
Linha := StringParaVetor(Memo1.Lines[i]);
NohNivel1 := nil;
NohNivel2 := nil;
AddNoh(NohNivel1, nil, Linha[0]);
AddNoh(NohNivel2, NohNivel1, Linha[1]);
ValueListEditor1.Strings.Add(Format('%s;%s;1=%s', [Linha[0], Linha[1], Linha[2]]));
ValueListEditor1.Strings.Add(Format('%s;%s;2=%s', [Linha[0], Linha[1], Linha[3]]));
ValueListEditor1.Strings.Add(Format('%s;%s;3=%s', [Linha[0], Linha[1], Linha[4]]));
end;
end;
Click of Treeview1
procedure TForm1.TreeView1Click(Sender: TObject);
begin
if TreeView1.Selected.Parent = nil then
begin
Exit;
end;
Memo2.Clear;
Memo2.Lines.Add(ValueListEditor1.Values[Format('%s;%s;1', [TreeView1.Selected.Parent.Text, TreeView1.Selected.Text])]);
Memo2.Lines.Add(ValueListEditor1.Values[Format('%s;%s;2', [TreeView1.Selected.Parent.Text, TreeView1.Selected.Text])]);
Memo2.Lines.Add(ValueListEditor1.Values[Format('%s;%s;3', [TreeView1.Selected.Parent.Text, TreeView1.Selected.Text])]);
end;
Function StringParaVetor
function StringParaVetor(const pString: String; const pDelimitador: Char = ';'): TVetorDeString;
var
i: Integer;
aux: string;
begin
SetLength(Result, 0);
aux := EmptyStr;
for i := 1 to Length(pString) do
begin
if pString[i] = pDelimitador then
begin
SetLength(Result, Length(Result)+1);
Result[High(Result)] := aux;
aux := EmptyStr;
Continue;
end;
aux := aux + pString[i]
end;
SetLength(Result, Length(Result)+1);
Result[High(Result)] := aux;
end;
Procedure AddNoh
procedure AddNoh(var pNoh: TTreeNode; const pNohPai: TTreeNode; const pTexto: String);
var
i: Integer;
begin
if pNohPai = nil then
begin
for i := 0 to TreeView1.Items.Count-1 do
begin
if TreeView1.Items[i].Text = pTexto then
begin
pNoh := TreeView1.Items[i];
Break;
end;
end;
if pNoh = nil then
begin
pNoh := TreeView1.Items.Add(pNohPai, pTexto);
end;
end
else
begin
pNoh := TreeView1.Items.AddChild(pNohPai, pTexto);
end;
end;
Explaining
Button1click
Traverse line-by-line of Stringlist of Memo1
for i := 0 to Memo1.Lines.Count-1 do
Each line has been transformed into a vector with the function Stringparavetor:
Vector position 1 is a first-level node, add it only if it has not yet been added using Procedure AddNoh
:
AddNoh(NohNivel1, nil, Linha[0]);
The vector position 2 is a second level node (child of the first level):
AddNoh(NohNivel2, NohNivel1, Linha[1]);
Positions 3, 4 and 5 are the fields to be saved in Valuelisteditor1, each field is saved in a row with a "key" that identifies each line:
ValueListEditor1.Strings.Add(Format('%s;%s;1=%s', [Linha[0], Linha[1], Linha[2]]));
ValueListEditor1.Strings.Add(Format('%s;%s;2=%s', [Linha[0], Linha[1], Linha[3]]));
ValueListEditor1.Strings.Add(Format('%s;%s;3=%s', [Linha[0], Linha[1], Linha[4]]));
This part can be optimized with a loop, for several lines.
Treeview1click
To show on Memo2 when clicking on the corresponding node, the idea is to search for the "key" stored in Valuelisteditor1, and add the corresponding Value.
what is the doubt to be certain?
– Tiago Rodrigues
I wanted to make the application read text files delimited by ; and read the data as it is in the image, only until then to read ta OK works, the fact is to put it in a Treeview, like each main Node be used the first TAG of each column for example "Email, Games, Steam" as Tags Parents or whatever the subitens would be just the rest of the specific information of each category
– Mk II Phobos LordMargatroid