Solved

Inheriting Properties...

Posted on 2002-05-02
8
189 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
 
LVL 8

Expert Comment

by:TOndrej
ID: 6988499
Assign should do the job, as DragonSlayer suggested.
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 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

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
Delphi component that can load a DLL in design time? 8 53
Delphi cmd execution 6 56
creating threads in delphi 1 82
TEMBEDDEDWB how can i change its user agent ? 8 48
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 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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

15 Experts available now in Live!

Get 1:1 Help Now