As you pointed out in the comments, that your difficulty is setting up a structure in TreeView , I'll leave a solution with that focus.
For the solution I used:
- A TMemo as the basis for the file.
- A TTreeView .
- A TValueListEditor to store the fields that will be outside the tree, invisible because it will only serve to store the temporary data.
- Other TMemo to display the fields of the selected node in the tree.
Codes
DeclaretypeTVetorDeString
typeTVetorDeString=arrayofstring;
Clickthe"Process" button
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;
TreeView1 Click
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
Go to the StringList line-by-line of the Memo1
for i := 0 to Memo1.Lines.Count-1 do
Each line has been transformed into a vector with the function StringParaVetor :
Position 1 of the vector is a first level node, I add it only if it has not yet been added using the procedure AddNoh
:
AddNoh(NohNivel1, nil, Linha[0]);
Position 2 of the vector is a second level node (first level child):
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 row:
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 multi-line loop.
TreeView1Click
To show in Memo2 when clicking on the corresponding node, the idea is to search for the "key" stored in ValueListEditor1, and add the corresponding Value.