Solved

Changed Properties

Posted on 2002-04-17
8
240 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to draw animated progressbar in Tvirtualstringtree 5 117
Magic Software info 18 126
How to renew a Delphi rad-studio licence? 5 49
Convert MS Word document to a PDF file 9 65
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

28 Experts available now in Live!

Get 1:1 Help Now