Solved

Open Delphi form in Treeview

Posted on 2004-04-29
9
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

752 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