?
Solved

Inheriting Properties...

Posted on 2002-05-02
8
Medium Priority
?
195 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 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