Doubt with handling of Treeview and delimited TXT files

Asked

Viewed 362 times

0

I’m having a little difficulty with Delphi, I’m creating a very basic small application for me to manage accounts, games, emails etc. and I decided to use the Treeview scheme as a base for listing accounts, but I’m having a great difficulty what I want to do is basically register files in a TXT, and have the application read the file delimited by ";" and it creates items and sub-items depending on what I want to view, as if each item is a category, like Emails, Games, etc... I’m going to send a Print of how I wanted to ask some questions if anyone can help me with that and know.. I appreciate it in advance inserir a descrição da imagem aqui

  • what is the doubt to be certain?

  • 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

1 answer

1


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.

Print de quais componentes usar no form

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.

  • Really that’s what I wanted, I appreciate the help, helped a lot, I always had doubts about how to use the Treeview suffered I lost a lot of time, but fortunately you cleared my doubts, I believe that many people also have difficulty using this component.

  • cool, good that I helped, success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.