Solved

Open Delphi form in Treeview

Posted on 2004-04-29
9
264 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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