Solved

Changed Properties

Posted on 2002-04-17
8
242 Views
Last Modified: 2013-11-23
Changed Properties

For example :
I create 5 new components in TComponentList at runtime.
Then change some properties.
TEdit(ComponentList[0]).Parent:=Form1;
TEdit(ComponentList[0]).Name:='Edit1';
TEdit(ComponentList[0]).Color:=clRed;

Result must be (in TList) :
Edit1.Parent:=Form1;
Edit1.Parent:=clRed;
0
Comment
Question by:vbmn
8 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6949244
I'm not sure if this correctly works but it may be a start to something:

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
  CName, CParent, CColor : string;
begin
  for I := 0 to ComponentCount - 1 do
  begin
    CName := TControl(Components[I]).Name;
    CParent := TWinControl(TControl(Components[I]).Parent).Name;
    CColor := ColorToString(TControl(Components[I]).Color);
    ListBox1.Items.Add(CName + '.Parent := ' + CParent + ';');
    ListBox1.Items.Add(CName + '.Color := ' + CColor + ';');
  end;
end;

Cheers,

Alan
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6949659
listening...for moment
0
 
LVL 9

Accepted Solution

by:
ITugay earned 300 total points
ID: 6949705
Hi vbmn,

Here is the way to show component properties. I take it from Delphi's help.

function ComponentToString(Component: TComponent): string;

var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;

    end;
  finally
    BinStream.Free
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);

begin
  // it show property of given component
  ShowMessage(ComponentToString(Memo1));

  // or whole form with all controls on it
  ShowMessage(ComponentToString(Form1));

  // you can easy add result to TStrings
  // MyList.Add(ComponentToString(Edit1));
end;

----
Igor.
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 2

Author Comment

by:vbmn
ID: 6949941
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    ComponentList:TComponentList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}


function ComponentToString(Component: TComponent): string;

var
 BinStream:TMemoryStream;
 StrStream: TStringStream;
 s: string;
begin
 BinStream := TMemoryStream.Create;
 try
   StrStream := TStringStream.Create(s);
   try
     BinStream.WriteComponent(Component);
     BinStream.Seek(0, soFromBeginning);
     ObjectBinaryToText(BinStream, StrStream);
     StrStream.Seek(0, soFromBeginning);
     Result:= StrStream.DataString;
   finally
     StrStream.Free;

   end;
 finally
   BinStream.Free
 end;
end;

function StringToComponent(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(nil);

    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ComponentList:=TComponentList.Create;
ComponentList.Add(TPanel.Create(self));
TPanel(ComponentList[0]).Parent:=Form1;
TPanel(ComponentList[0]).Name:='Panel1';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 RichEdit1.Clear;
 RichEdit1.Text:=(ComponentToString(TPanel(ComponentList[0])));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//  E R R O R
//  E R R O R
//  E R R O R
//  E R R O R
//  E R R O R
//Error EClassNotFound.
StringToComponent(RichEdit1.Text);
end;

end.
0
 
LVL 2

Author Comment

by:vbmn
ID: 6949942
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    ComponentList:TComponentList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}


function ComponentToString(Component: TComponent): string;

var
 BinStream:TMemoryStream;
 StrStream: TStringStream;
 s: string;
begin
 BinStream := TMemoryStream.Create;
 try
   StrStream := TStringStream.Create(s);
   try
     BinStream.WriteComponent(Component);
     BinStream.Seek(0, soFromBeginning);
     ObjectBinaryToText(BinStream, StrStream);
     StrStream.Seek(0, soFromBeginning);
     Result:= StrStream.DataString;
   finally
     StrStream.Free;

   end;
 finally
   BinStream.Free
 end;
end;

function StringToComponent(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(nil);

    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ComponentList:=TComponentList.Create;
ComponentList.Add(TPanel.Create(self));
TPanel(ComponentList[0]).Parent:=Form1;
TPanel(ComponentList[0]).Name:='Panel1';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 RichEdit1.Clear;
 RichEdit1.Text:=(ComponentToString(TPanel(ComponentList[0])));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//  E R R O R
//  E R R O R
//  E R R O R
//  E R R O R
//  E R R O R
//Error EClassNotFound.
StringToComponent(RichEdit1.Text);
end;

end.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6949977
You have to register class.

Delphi's help:


Registers a class of persistent object so that it’s class type can be retrieved.

procedure RegisterClass(AClass: TPersistentClass);

Description

Call RegisterClass to register a class with the streaming system. Form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered.  Any other classes used by an application must be explicitly registered by calling RegisterClass if instances are to be saved.

Once classes are registered, they can be loaded or saved by the VCL streaming system. GetClass returns nil when passed the class name of an unregistered class, and FindClass raises an exception for unregistered classes.

The AClass parameter is the class that is descended from TPersistent. Put the call to RegisterClass in a Register procedure or in the initialization section of the unit in which the class is defined.

If the class is already registered, RegisterClass does nothing. If a different class with the same name is already registered, RegisterClass raises an EFilerError exception.

Note:     Registering a component using the RegisterNoIcon or RegisterComponents method does not automatically register the class. It is still necessary to call RegisterClass for components.
0
 
LVL 2

Author Comment

by:vbmn
ID: 6950059
Could You give me an example ? I don't know how to use it.

Thanks.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6950081
add RegisterClass somewhere in project. It must be called before you call streaming mechanizm.

e.g:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComponentList:=TComponentList.Create;
  ComponentList.Add(TPanel.Create(self));
  TPanel(ComponentList[0]).Parent:=Form1;
  TPanel(ComponentList[0]).Name:='Panel1';

  RegisterClass(TPanel);

 
end;
0

Featured Post

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.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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