Solved

Open Delphi form in Treeview

Posted on 2004-04-29
9
260 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now