Solved

Changed Properties

Posted on 2002-04-17
8
241 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
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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…

816 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

11 Experts available now in Live!

Get 1:1 Help Now