[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Inheriting Properties...

Posted on 2002-05-02
8
Medium Priority
?
196 Views
Last Modified: 2010-04-04
I've just had a question answered rather nicley here, about dynamically created component properties.

Now I realise that I need to ask another question... then I should be done. :-)

I have some dynamically created (at run-time) TPanel components, and am using the :

(componentName as TPanel).property := value;

.... to access the properties.

Is there a way that I can perhaps copy across all properties from an existing TPanel, to the newly dynamically created components, and then just change the few values that I need to change for each?

For example, I have one TPanel that I create at design time, and would like all those properties to pass onto the dynamically created TPanels..... then, I only need to change two or three properties on each, individually.

Cheers.
0
Comment
Question by:nicey
[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 14

Accepted Solution

by:
DragonSlayer earned 200 total points
ID: 6987692
Umm... try something like this?

MyNewPanel := TPanel.Create(Self);
MyNewPanel.Assign(ThePanelThatIsAlreadyCreated);



HTH
DragonSlayer
0
 
LVL 17

Expert Comment

by:inthe
ID: 6987966
hi,

or create a procedre/function to create the panel and pass
the parameters that can change in the arguments and set the rest in the create procedure:



public
Procedure CustomClick(Sender:TObject);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.CustomClick(Sender:TObject);
begin
showmessage(ColorToString((Sender as TPanel).color));
end;


procedure createpanel(pcolor : TColor;ptop,pleft,pwidth,pheight : integer);
var
  panels : tpanel;
begin
 panels := tpanel.Create(nil);
 with panels do begin
 Color   := pColor;  //custom settings
 Top     := pTop;
 Left    := pLeft;
 Width   := pWidth;
 Height  := pHeight;
                    //global settings
 Hint        := 'some hint';
 ShowHint    := True;
 BorderStyle := bsSingle;
 Caption :=  'i am a new panel';
 OnClick     := Form1.CustomClick;
 Parent := Form1;
 end;
 panels.Show;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
createpanel(clBlue,20,20,100,50);
createpanel(clRed,100,100,200,75);
end;
0
 
LVL 1

Expert Comment

by:piscean
ID: 6988302
if i'm not mistaken, there's already an answered question similar to this, "copying an object to another". why don't you try a search ...
0
Industry Leaders: 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 8

Expert Comment

by:TOndrej
ID: 6988499
Assign should do the job, as DragonSlayer suggested.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6988662
Have had this around for a while... forgot where I got it from:


procedure CopyObject(ObjFrom, ObjTo: TObject);
var
  PropInfos: PPropList;
  PropInfo: PPropInfo;
  Count, Loop: Integer;
  OrdVal: Longint;
  StrVal: String;
  FloatVal: Extended;
  MethodVal: TMethod;
begin
  { Iterate thru all published fields and properties of source }
  { copying them to target }

  { Find out how many properties we'll be considering }
  Count := GetPropList(ObjFrom.ClassInfo, tkAny, nil);
  { Allocate memory to hold their RTTI data }
  GetMem(PropInfos, Count * SizeOf(PPropInfo));
  try
    { Get hold of the property list in our new buffer }
    GetPropList(ObjFrom.ClassInfo, tkAny, PropInfos);
    { Loop through all the selected properties }
    for Loop := 0 to Count - 1 do
    begin
      PropInfo := GetPropInfo(ObjTo.ClassInfo, PropInfos^[Loop]^.Name);
      { Check the general type of the property }
      { and read/write it in an appropriate way }
      case PropInfos^[Loop]^.PropType^.Kind of
        tkInteger, tkChar, tkEnumeration,
        tkSet, tkClass{$ifdef Win32}, tkWChar{$endif}:
        begin
          OrdVal := GetOrdProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) then
            SetOrdProp(ObjTo, PropInfo, OrdVal);
        end;
        tkFloat:
        begin
          FloatVal := GetFloatProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) then
            SetFloatProp(ObjTo, PropInfo, FloatVal);
        end;
        {$ifndef DelphiLessThan3}
        tkWString,
        {$endif}
        {$ifdef Win32}
        tkLString,
        {$endif}
        tkString:
        begin
          { Avoid copying 'Name' - components must have unique names }
          if UpperCase(PropInfos^[Loop]^.Name) = 'NAME' then
            Continue;
          StrVal := GetStrProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) then
            SetStrProp(ObjTo, PropInfo, StrVal);
        end;
        tkMethod:
        begin
          MethodVal := GetMethodProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) then
            SetMethodProp(ObjTo, PropInfo, MethodVal);
        end
      end
    end
  finally
    FreeMem(PropInfos, Count * SizeOf(PPropInfo));
  end;
end;

USAGE:

Pnl:=TPanel.Create(self);
Pnl.Parent:=self;

CopyObject(Panel1,pnl);




Good luck!!


0
 
LVL 1

Author Comment

by:nicey
ID: 6988996
DragonSlayer, seems that plan is the simplest, and does seem to be getting me somwhere... but, it's raising Exceptions before the program will run.... the debugger then sits at the Application.Run in the main project file?
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6989410
umm... where did you put the statements?
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004521
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from DragonSlayer

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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