MartinC
asked on
Dynamic Creation of Objects
I need to create DYNAMICALLY some objects within other objects. The pseudocode I want goes something like this:
// on click of a button
For iLoop := 1 to iNoOfPanelsToDo do
begin
// Create a panel
// give it a cyclic name ('panel001')
// set its size and location on the form by Top := iLoop*20 etc.
// Create a child panel on the above panel
// give it a cyclic name ('innerpanel001')
// set its size and location on the form
// Create a button on the innerpanel
// Attach procedure 'InnerButtonPressed' to the button's OnClick event
end;
I've stumbled through the Delphi Help but I cant seem to find anything like this in terms I've guessed to put in the search, yet I suspect it is something the more experienced Delphi people will know like the back of their hand. I did it before ages ago; all I can remember is the bit about accessing the dynamically-created components by name with FindComponent.
Sample code is best; I learn well from plagiarism; oops, I mean example.
Martin C
// on click of a button
For iLoop := 1 to iNoOfPanelsToDo do
begin
// Create a panel
// give it a cyclic name ('panel001')
// set its size and location on the form by Top := iLoop*20 etc.
// Create a child panel on the above panel
// give it a cyclic name ('innerpanel001')
// set its size and location on the form
// Create a button on the innerpanel
// Attach procedure 'InnerButtonPressed' to the button's OnClick event
end;
I've stumbled through the Delphi Help but I cant seem to find anything like this in terms I've guessed to put in the search, yet I suspect it is something the more experienced Delphi people will know like the back of their hand. I did it before ages ago; all I can remember is the bit about accessing the dynamically-created components by name with FindComponent.
Sample code is best; I learn well from plagiarism; oops, I mean example.
Martin C
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
well, glad to hear
about the error you get
redefine your proc to
procedure TFormClassName.YourPredefi nedClickPr oc(Sender : TObject);
begin
beep;
beep;
beep;
end;
you may notice TFormClassName
this means you should declare this procedure within your formtype like
type
TfrmAssetFacet = class(TForm)
.....
private //could also public
procedure YourPredefinedClickProc(Se nder : TObject);
......
end;
so that your proc becomes
procedure TfrmAssetFacet.YourPredefi nedClickPr oc(Sender : TObject);
begin
beep;
beep;
beep;
end;
hope you get it work
meikl ;-)
about the error you get
redefine your proc to
procedure TFormClassName.YourPredefi
begin
beep;
beep;
beep;
end;
you may notice TFormClassName
this means you should declare this procedure within your formtype like
type
TfrmAssetFacet = class(TForm)
.....
private //could also public
procedure YourPredefinedClickProc(Se
......
end;
so that your proc becomes
procedure TfrmAssetFacet.YourPredefi
begin
beep;
beep;
beep;
end;
hope you get it work
meikl ;-)
ASKER
I tried that before ... I have the declaration like this:
type
TfrmAssetFacet = class(TForm)
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure YourPredefinedClickProc;
end;
... then in the code I have:
onClick := YourPredefinedClickProc;
... and later I have:
procedure TfrmAssetFacet.YourPredefi nedClickPr oc;
begin
beep;
beep;
beep;
end;
... but the compiler says:
[Error] uAssetFacet.pas(68): Incompatible types: 'Parameter lists differ'
... any clues?
type
TfrmAssetFacet = class(TForm)
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure YourPredefinedClickProc;
end;
... then in the code I have:
onClick := YourPredefinedClickProc;
... and later I have:
procedure TfrmAssetFacet.YourPredefi
begin
beep;
beep;
beep;
end;
... but the compiler says:
[Error] uAssetFacet.pas(68): Incompatible types: 'Parameter lists differ'
... any clues?
an easy one (you just not read my comment carfully ;-)
replace this
procedure TfrmAssetFacet.YourPredefi nedClickPr oc;
begin
beep;
beep;
beep;
end;
with
procedure TfrmAssetFacet.YourPredefi nedClickPr oc(Sender : TObject); //just add the sender
begin
beep;
beep;
beep;
end;
hope now you get it work
meikl ;-)
replace this
procedure TfrmAssetFacet.YourPredefi
begin
beep;
beep;
beep;
end;
with
procedure TfrmAssetFacet.YourPredefi
begin
beep;
beep;
beep;
end;
hope now you get it work
meikl ;-)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
good point, ZhaawZ, overlooked this
ASKER
Sorry, normally I am a good copier but I missed that one. I've increased the points a bit (60 to 100) and chucked a tidbit to ZhaawZ for his correction, which no doubt would have tripped me up too.
Martin
Martin
ASKER
I got all that to work - thx! - except the line:
onClick := YourPredefinedClickProc;
which says "[Error] uAssetFacet.pas(67): Incompatible types: 'method pointer and regular procedure'" ... I presume there is some way I am not defining the procedure ... I wrote it as:
procedure YourPredefinedClickProc;
begin
beep;
beep;
beep;
end;
... and I have no idea where I am supposed to store its top line:
procedure YourPredefinedClickProc;
... which I cuurently have above the form's type declaration:
type
TfrmAssetFacet = class(TForm)
... any idea where it should go?