Solved

Changed Properties

Posted on 2002-04-17
8
239 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

16 Experts available now in Live!

Get 1:1 Help Now