AS: during the communication with topic starters answer grown too. The total outcome is like http://pastebin.ca/2426760
procedure TForm1.CreateButton(VAR Button: TButton; CONST L: Integer; CONST T: Integer);
That is of basics of Pascal language how to pass parameters to procedures/functions.
http://docwiki.embarcadero.com/RADStudio/XE4/en/Parameters_(Delphi)
Actually, I don't think there is any problem with the parameters
Button = nil, which means the values of "Button1" and "Button2" are not sent, however
http://pastebin.ca/2427238
Kudoes to Bill for spotting this. Using separate properties to position your controls is both inefficient and prone to copy-paste errors.
Using the 2nd link:
procedure TForm1.CreateButton(out Button: TButton; const L: Integer; const T: Integer);
begin
Button:= TButton.Create(Self);
Button.Parent:= Self;
Button.SetBounds( L, T, 100, 50);
end;
Actually what do you do with pointers to newly created buttons ?
In your code you just loose them!
procedure TForm1.FormCreate(Sender: TObject);
Var
Button1, Button2: TButton;
begin
...
end;
In this your code those pointers would be just lost! If you do need those values - pass them outside of the procedure. If you do not - do not ask for them - http://en.wikipedia.org/wiki/YAGNI http://en.wikipedia.org/wiki/KISS_principle
Procedure TForm1.CreateButton(const L, T: Integer);
begin
With TButton.Create(Self) do begin
Parent := Self;
SetBounds( L, T, 100, 50);
Caption := 'Caption at ' + IntToStr(T);
Name := 'Name at ' + IntToStr(L);
End;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
B1.CreateButton( 100, 50); //Sending properties
B2.CreateButton( 200, 40); //Sending properties
end;
Now to those B1, B2...
You claim that you want 2 buttons on the form, but you code shows you try to make THREE FORMS and one button on the 2nd form and one button on the 3rd form. So what do you really want ??? And do you check that B1 and B2 forms were created befoe tryign to add buttons to them ?
Perhaps you really wanted
procedure TForm1.FormCreate(Sender: TObject);
begin
SELF.CreateButton( 100, 50); //Sending properties
SELF.CreateButton( 200, 40); //Sending properties
end;
Then to go with DRY principle and to keep all the variables in one place.
http://docwiki.embarcadero.com/Libraries/XE2/en/System.Types.TPoint
Procedure TForm1.CreateButtons(const a: array of TPoint);
Var p: TPoint;
Begin
for p in a do
CreateButton( p.x, p.y );
End;
type TPointDynArray = array of TPoint;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateButtons( TPointDynArray.Create(
Point( 100,50 ), Point(200, 40) ) );
end;
Kudos to Delphi array initialization
Later you can always add more coordinates to an array and keep it consistent.
Well, to bring down this to Delphi 7 abilities that would - somewhat redundantly - be coded like
const BtnCnt = 2;
BtnLocations : array [1..BtnCnt] of TSize = (
( cx: 100, cy: 50 ), ( cx: 200, cy: 40 )
);
Procedure TForm1.CreateButtons(const a: array of TSize);
Var i: integer;
Begin
for i := Low(a) to High(a) do
with a[i] do
CreateButton( cx, cy );
End;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateButtons( BtnLocations );
end;
But while Delphi 5 and Dephi 7 were great releases, they are very outdated. I definitely suggest you either upgradeing to Delphi XE or more recent, or side-stepping to CodeTyphon
TForm1 = class(TForm) //Declaring the procedure
procedure CreateButton(Button: TButton; L: Integer; T: Integer);
Declaring that one-purpose procedure in PUBLISHED section of the form class is also not a very good style. You'd better declare them in PRIVATE section. Adhering to "least visibility" would help you to make interdependencies controllable. Otherwise in a year your program would become a spaghetti mess, where you just cannot change anything without ruining everything else. I am workign on a project with 10+ years of history now and i see the consequences of "everything is public" very clear. It is a great pain!