Solved

Inheriting Properties...

Posted on 2002-05-02
8
191 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
8 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 50 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 322
Magic Software info 18 138
Delphi 2 68
IdTCPClient and IdTCPServer exchanging data on desktops and not Android 11 140
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…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 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