Solved

Inheriting Properties...

Posted on 2002-05-02
8
188 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

22 Experts available now in Live!

Get 1:1 Help Now