Solved

Open Delphi form in Treeview

Posted on 2004-04-29
9
267 Views
Last Modified: 2010-04-05
I wish to have a form with a button & a file open dialog box. When the button is clicked a form can be chosen using the open dialog box & the components listed in a treeview with the correct parent child relationships between the forms components.

Any idea's ?

Thanks
Paul.
0
Comment
Question by:pr_wainwright
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10946909

  for counter := 0 to Form1.ComponentCount-1 do
    begin
      ComponentName := (Form1.Components[counter] as TComponent).Name;
      OwnerName := (Form1.Components[counter].Owner as TComponent).Name;
      // add to tree view ...
    end;

  but I don't have ide how you will open the DFM cause it is not in readable format ....
0
 
LVL 1

Author Comment

by:pr_wainwright
ID: 10947807
Ivanov G,
 I think from Delphi 4 or 5 onwards, can't remember which form files are stored as text files.

Regards
Paul.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10947945

   Yep, I checked it on D7 - it is text file ... but parsing the text is not so easy...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Expert Comment

by:pritaeas
ID: 10948141
Actually, you can read the form's text file into a stream and use ObjectTextToBinary to convert that text into an actual form. Then you can use the code above, to loop through all the components and display them.
0
 
LVL 1

Author Comment

by:pr_wainwright
ID: 10951033
pritaeas,
            I can't seem to get your idea to work using ObjectTextToBinary. Do you have a source code example to help me along ?.

Thanks
Paul.
0
 
LVL 6

Expert Comment

by:pritaeas
ID: 10968775
Sorry, I can't find my original source anymore. I'll try to recreate an example, then I'll get back to you.
0
 
LVL 6

Expert Comment

by:pritaeas
ID: 10972872
Hi. Tried to recreate my idea, but it doesn't work. This comes because the type of form I want to create is not registered at that time.

However, looking throught the .dfm file I noticed that every 'object' is as text inside the parent object. Each nesting indents with 2 spaces. This should be enough to recreate a tree. Just load the .dfm into a stringlist. Delete all lines NOT beginning with 'object' (ignore leading spaces). Then build a tree from that textfile.

hth, pritaeas
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10975141
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    CheckBox1: TCheckBox;
    Button2: TButton;
    Panel1: TPanel;
    Memo1: TMemo;
    TreeView1: TTreeView;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure AddToList(ParentName: String; Childs: TStringList);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure EnumChilds (Parent : TComponent; var ChildList : TStringList);
var
  counter     : Integer;
  name        : String;
begin
  if Parent.ComponentCount > 0 then
    for counter := 0 to Parent.ComponentCount - 1 do
      begin
        name := (Parent.Components[counter] as TComponent).Name;
        ChildList.Add(name);
      end;
  Form1.AddToList(Parent.Name, ChildList);
  ChildList.Clear;
end;

procedure TForm1.AddToList(ParentName: String; Childs: TStringList);
var
  childnode : TTreeNode;
  i : integer;
begin
  childnode := TTreeNode.Create(TreeView1.items);
  TreeView1.Items.AddChild(childnode, parentName);
  for i := 0 to childs.count - 1 do
    TreeView1.Items.AddChild(childnode, childs[i]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  List : TStringList;
begin
  List := TStringList.Create;
  EnumChilds(Self, List);
end;

end.
0
 
LVL 6

Accepted Solution

by:
pritaeas earned 125 total points
ID: 11045079
Hi Paul,

sorry for the very late reply, but I hope you can use it. Download the demo-project from my website at:

http://pritaeas.net/demo_projects/dfm2tree.zip

hth, Hans 'pritaeas' Pollaerts
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question