1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram

  5. Delphifan Magazine
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Ques/Help/Req Can you check this code

Discussion in 'Delphi Help&Requests' started by XeGregory, Jul 27, 2018.

  1. XeGregory
    Offline

    XeGregory Delphifan - Kings of programming

  2. XeGregory
    Offline

    XeGregory Delphifan - Kings of programming

    Code:
    type
      TNodeData = class
    
      private
        Lines: TStringList;
        Level: Byte;
      end;
    
    type
      TFDataTree = class(TForm)
        MainMenu: TMainMenu;
        { Ajouter un noeud }
        AddNoeud: TMenuItem;
        { Ajouter un sous-noeud }
        AddSsNoeud: TMenuItem;
        { Supprimer le noeud }
        DeleteNoeud: TMenuItem;
        { Enregistrer }
        SaveNoeud: TMenuItem;
        { TTreeView }
        TreeView: TTreeView;
        { TMemo }
        Memo: TMemo;
    
        { Form OnCreate }
        procedure FormCreate(Sender: TObject);
        { TreeView OnClick }
        procedure TreeViewClick(Sender: TObject);
        { TreeView OnDeletion }
        procedure TreeViewDeletion(Sender: TObject; Node: TTreeNode);
        { AddNoeud OnClick }
        procedure AddNoeudClick(Sender: TObject);
        { AddSsNoeud OnClick }
        procedure AddSsNoeudClick(Sender: TObject);
        { DeleteNoeud OnClick }
        procedure DeleteNoeudClick(Sender: TObject);
        { SaveNoeud OnClick }
        procedure SaveNoeudClick(Sender: TObject);
    
      private { Déclarations privées }
        // CurrentNode: TTreeNode;
        CurrentNodeData: TNodeData;
        { CreateDataFile }
        procedure CreateDataFile;
        { OpenDataFile }
        procedure OpenDataFile;
        { SaveDataFile }
        procedure SaveDataFile;
    
      end;
    
    var
      FDataTree: TFDataTree;
    
    const
      DataFile = 'DataTree.txt';
    
      SubNode = 'Node[';
      SubData = 'Data[';
      SubEnd = ']';
      SubArb = ' ';
    
    implementation
    
    {$R *.dfm}
    
    { SortTreeView }
    function SortTreeView(Node1, Node2: TTreeNode; Data: Integer): Integer; stdcall;
    begin
      Result := Data * AnsiCompareStr(Node1.Text, Node2.Text);
    end;
    
    { Private : CreateDataFile }
    procedure TFDataTree.CreateDataFile;
    var
      IDataFile: TStringList;
    begin
      if FileExists(DataFile) then
        Exit;
    
      IDataFile := TStringList.Create;
      with IDataFile do
      begin
        Add(SubNode + 'Root' + SubEnd);
        Add(SubData);
        Add(SubEnd);
      end;
    
      try
        IDataFile.SaveToFile(DataFile);
      except
        ShowMessage('Erreur impossible de créer le fichier DataTree.txt');
        FreeAndNil(IDataFile);
        Application.Terminate;
      end;
    
      FreeAndNil(IDataFile);
    end;
    
    { Private : OpenDataFile }
    procedure TFDataTree.OpenDataFile;
    var
      TreeData: TStringList;
      INode: Integer;
      DataText, NameNode: String;
      NodeData: TNodeData;
      TreeNode: TTreeNode;
      NodeBegin, DataEnd: Boolean;
    begin
      CreateDataFile;
    
      NodeBegin := False;
      TreeData := TStringList.Create;
      TreeData.LoadFromFile(DataFile);
    
      INode := 1;
      while INode <= TreeData.Count do
      begin
        DataText := TreeData[INode - 1];
    
        if AnsiLeftStr(DataText, 5) = SubNode then { Node[ }
        begin
          NodeData := TNodeData.Create;
          NodeData.Lines := TStringList.Create;
    
          DataText := AnsiMidStr(DataText, 6, Length(DataText) - 6);
          NameNode := TrimLeft(DataText);
          NodeData.Level := 1 + Length(DataText) - Length(NameNode);
        end
        else if AnsiLeftStr(DataText, 5) = SubData then { Data[ }
        begin
          Inc(INode);
          DataEnd := False;
    
          while (not DataEnd) and (INode <= TreeData.Count) do
          begin
            DataText := TreeData[INode - 1];
    
            if DataText <> SubEnd then { ] }
            begin
              NodeData.Lines.Add(DataText);
              Inc(INode);
            end
            else
            begin
              DataEnd := True;
    
              if not NodeBegin then
              begin
                TreeNode := TreeView.Items.Add(TreeView.TopItem, NameNode);
                NodeBegin := True;
              end
              else
              begin
                if NodeData.Level > TNodeData(TreeNode.Data).Level then
                  TreeNode := TreeView.Items.AddChild(TreeNode, NameNode)
                else if NodeData.Level = TNodeData(TreeNode.Data).Level then
                  TreeNode := TreeView.Items.Add(TreeNode, NameNode)
                else
                begin
                  repeat
                    TreeNode := TreeNode.Parent;
                  until (NodeData.Level = TNodeData(TreeNode.Data).Level);
    
                  TreeNode := TreeView.Items.Add(TreeNode, NameNode);
                end;
              end;
    
              TreeNode.Data := NodeData;
            end;
          end; { while }
        end;
    
        Inc(INode);
      end; { while }
    
      if TreeView.Items.Count > 0 then
      begin
        TreeView.Selected := TreeView.Items.GetFirstNode;
        CurrentNodeData := TNodeData(TreeView.Selected.Data);
        Memo.Text := CurrentNodeData.Lines.Text;
      end;
    
      TreeView.CustomSort(@SortTreeView, 1);
      FreeAndNil(TreeData);
    end;
    
    { Private : SaveDataFile }
    procedure TFDataTree.SaveDataFile;
    var
      TreeData: TStringList;
      TreeNode: TTreeNode;
      NodeData: TNodeData;
      INode, Row: Integer;
    begin
      CurrentNodeData.Lines.Clear;
      CurrentNodeData.Lines.AddStrings(Memo.Lines);
    
      TreeData := TStringList.Create;
    
      if TreeView.Items.Count > 0 then
      begin
        for INode := 1 to TreeView.Items.Count do
        begin
          TreeNode := TreeView.Items[INode - 1];
          NodeData := TNodeData(TreeNode.Data);
    
          for Row := 1 to NodeData.Lines.Count do
            NodeData.Lines[Row - 1] := NodeData.Lines[Row - 1];
    
          with TreeData do
          begin
            Add(SubNode + StringOfChar(SubArb, NodeData.Level - 1) + TreeNode.Text + SubEnd);
            Add(SubData);
            Add(NodeData.Lines.Text);
            Add(SubEnd);
          end;
        end;
      end;
    
      TreeData.SaveToFile(DataFile);
      FreeAndNil(TreeData);
    end;
    
    { Form OnCreate }
    procedure TFDataTree.FormCreate(Sender: TObject);
    begin
      OpenDataFile;
    end;
    
    { TreeView OnClick }
    procedure TFDataTree.TreeViewClick(Sender: TObject);
    begin
      if TreeView.Items.Count > 0 then
      begin
        CurrentNodeData.Lines.Clear;
        CurrentNodeData.Lines.AddStrings(Memo.Lines);
        CurrentNodeData := TNodeData(TreeView.Selected.Data);
        Memo.Text := CurrentNodeData.Lines.Text;
      end
      else
        Memo.Clear;
    end;
    
    { TreeView OnDeletion }
    procedure TFDataTree.TreeViewDeletion(Sender: TObject; Node: TTreeNode);
    var
      NodeData: TNodeData;
    begin
      NodeData := TNodeData(Node.Data);
      FreeAndNil(NodeData);
    end;
    
    { AddNoeud OnClick }
    procedure TFDataTree.AddNoeudClick(Sender: TObject);
    var
      NameNode: String;
      NodeData: TNodeData;
      TreeNode: TTreeNode;
    begin
      NodeData := TNodeData.Create;
      NodeData.Lines := TStringList.Create;
      NameNode := EmptyStr;
    
      InputQuery('Ajouter un noeud', 'Noeud :', NameNode);
    
      if (Trim(NameNode) <> EmptyStr) and (Length(NameNode) < 255) then
      begin
        if TreeView.Items.Count = 0 then
        begin
          NodeData.Level := 0;
          TreeView.Selected := TreeView.TopItem;
        end
        else
          NodeData.Level := CurrentNodeData.Level;
    
        TreeNode := TreeView.Items.Insert(TreeView.Selected, Trim(NameNode));
        TreeNode.Data := NodeData;
    
        TreeView.Selected := TreeNode;
        TreeViewClick(TreeView);
        TreeView.CustomSort(@SortTreeView, 1);
    
        SaveDataFile;
        Memo.SetFocus;
      end;
    end;
    
    { AddNoeud OnClick }
    procedure TFDataTree.AddSsNoeudClick(Sender: TObject);
    var
      NameSsNode: String;
      NodeData: TNodeData;
      TreeNode: TTreeNode;
    begin
      NodeData := TNodeData.Create;
      NodeData.Lines := TStringList.Create;
    
      InputQuery('Ajouter un sous-noeud', 'Sous-noeud :', NameSsNode);
    
      if (Trim(NameSsNode) <> EmptyStr) and (Length(NameSsNode) < 255) then
      begin
        if TreeView.Items.Count > 0 then
        begin
          NodeData.Level := CurrentNodeData.Level + 1;
          TreeNode := TreeView.Items.AddChildFirst(TreeView.Selected, Trim(NameSsNode));
          TreeNode.Data := NodeData;
    
          TreeView.Selected := TreeNode;
          TreeViewClick(TreeView);
          TreeView.CustomSort(@SortTreeView, 1);
    
          SaveDataFile;
          Memo.SetFocus;
        end;
      end;
    end;
    
    { DeleteNoeud OnClick }
    procedure TFDataTree.DeleteNoeudClick(Sender: TObject);
    begin
      if TreeView.Items.Count > 0 then
      begin
        TreeView.Selected.DeleteChildren;
        TreeView.Selected.Delete;
    
        TreeView.Selected := TreeView.TopItem;
        TreeView.Selected := TreeView.Selected;
    
        if TreeView.Items.Count > 0 then
        begin
          CurrentNodeData := TNodeData(TreeView.Selected.Data);
          TreeViewClick(TreeView);
        end;
    
        SaveDataFile;
      end;
    end;
    
    { SaveNoeud OnClick }
    procedure TFDataTree.SaveNoeudClick(Sender: TObject);
    begin
      SaveDataFile;
    end;
    Error Add Noeud, Add ss Noeud, Delete Noeud
     

    Attached Files:

  3. XeGregory
    Offline

    XeGregory Delphifan - Kings of programming

    My correction
     

    Attached Files:

    momo and AdminDF like this.
Tags:

Share This Page