?
Solved

Changed Properties

Posted on 2002-04-17
8
Medium Priority
?
244 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
[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
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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

762 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