Board index » delphi » creating new buttons

creating new buttons

I have one question.
I have created a form on which I have insert some buttons and when the
program is running I like to create more buttons .
Could someone help me ?

Reply address: bro...@ibm.net

 

Re:creating new buttons


Quote
Braun Roger <bro...@ibm.net> wrote:
>I have one question.
>I have created a form on which I have insert some buttons and when the
>program is running I like to create more buttons .
>Could someone help me ?
>Reply address: bro...@ibm.net

var
   NewButton: TButton;
Begin
   NewButton := TButton.Create(Form1)
   NewButton.Left := ...
   ...
   NewButton.Visible := True;
End;

Don't think I left any necessary steps out there. Could be wrong
however.

Cheers,
Damien

========================================================
Are you a shareware programmer? E-mail me for info on
something I'm writing to make your life a lot easier!!!
=== "http://204.101.50.93/students/damien/dmhome.htm" ===

Re:creating new buttons


Quote
Braun Roger <bro...@ibm.net> wrote:
>I have one question.
>I have created a form on which I have insert some buttons and when the
>program is running I like to create more buttons .
>Could someone help me ?
>Reply address: bro...@ibm.net

procedure TMyForm.CreateAButton;
var
  MyButton1 : TButton;
begin
  MyButton1:=TButton.Create(self);
  MyButton1.Parent:=ParentForm;  (or a panel or any other object that
has a window handle)
  MyButton.Left:= some number;
  MyButton.Top:= some number;
  MyButton.Caption:= some string;

   etc.

and don't forget to free the button when you are done with it.

MyButton1.Free;

If you don't set the parent for the button, it will not display.  

-------------------------------
Thomas Paul
TOMG...@Pipeline.Com
Dean Witter, Discover & Co.
New York City, NY USA
----------------------------------

Re:creating new buttons


In article <4psks9$...@news1.t1.usa.pipeline.com>, Thomas Paul
<TOMG...@PIPELINE.COM> writes

Quote

>procedure TMyForm.CreateAButton;
>var
>  MyButton1 : TButton;
>begin
>  MyButton1:=TButton.Create(self);
>  MyButton1.Parent:=ParentForm;  (or a panel or any other object that
>has a window handle)
>  MyButton.Left:= some number;
>  MyButton.Top:= some number;
>  MyButton.Caption:= some string;

>   etc.

>and don't forget to free the button when you are done with it.

>MyButton1.Free;

You don't actually free the button, the form will free it when it
destroys itself.

Just I'd let you know.

--
Nicholas Robinson

Re:creating new buttons


In article <31c1cb9...@endeavor.newcomm.net>, dmsm...@voyager.newcomm.net

Quote
(Damien Smith) writes:
>Subject:    Re: creating new buttons
>From:       dmsm...@voyager.newcomm.net (Damien Smith)
>Date:       Wed, 12 Jun 1996 19:43:27 GMT

>Braun Roger <bro...@ibm.net> wrote:

>>I have one question.
>>I have created a form on which I have insert some buttons and when the
>>program is running I like to create more buttons .
>>Could someone help me ?

>>Reply address: bro...@ibm.net

>var
>   NewButton: TButton;
>Begin
>   NewButton := TButton.Create(Form1)
>   NewButton.Left := ...
>   ...
>   NewButton.Visible := True;
>End;

>Don't think I left any necessary steps out there. Could be wrong
>however.

>Cheers,
>Damien

There's a little more to it than that.  Make sure you also set the Parent
property.  Here's some code from an actual program of mine.  It creates a
pair of buttons that are the same size and stacked vertically:

  {create the OnLine/OffLine button}
  OnLineBtn := TButton.Create(Self);
  with OnLineBtn do begin
    Parent:=Self;
    Font := LEDmsg.Font;   {a Label that was defined earlier in the
procedure}
    Top:=33;
    Left:=5;
    Height:=20;
    OnClick:=OnLineButtonClick;   {what to call when the button gets
clicked}
    Width:=Self.Width-10;  {the width of the parent component, a TBevel,
minus 10 pixels.  Notice that the Left position is 5, making the botton
centered withing the Bevel}
  end;

  {create the Config button}
  ConfigBtn := TButton.Create(Self);
  with ConfigBtn do begin
    Parent:=Self;
    Font := OnLineBtn.Font;
    Top:=OnLineBtn.Top+OnLineBtn.Height+3;  {just below the OnLine button,
created above}
    Left:=OnLineBtn.Left;
    Height:=OnLineBtn.Height;
    Width:=OnLineBtn.Width;
    Caption:='Setup';   {yes, I know this the "Configuration" button, but
"Setup" fits better}
    OnClick:=ConfigBtnClick;
  end;

You can call Show for each if you wish, but you might be happier calling
the Parent's Show method and having everything display at once.  

Because you have assigned these two components to a Parent, the
destruction of the Parent should result in the destruction of the
children.  If you need to dynamically destroy the components the same way
you created them, use the following:

OnLineBtn.Destroy;
ConfigBtn.Destroy;

Happy coding.....

- Kevin Pierce
As always, praise, flames and/or cash are welcome.

Re:creating new buttons


Quote
Nicholas Robinson <NRo...@demon.com> wrote:
>You don't actually free the button, the form will free it when it
>destroys itself.
>Just I'd let you know.

Nicholas is of course correct.  I have just gotten so paranoid about
freeing Delphi objects that my rule is to Free anything I Create.
-------------------------------
Thomas Paul
TOMG...@Pipeline.Com
Dean Witter, Discover & Co.
New York City, NY USA
----------------------------------

Re:creating new buttons


In article <4q4gli$...@news1.t1.usa.pipeline.com>, Thomas Paul
<TOMG...@PIPELINE.COM> writes

Quote
>Nicholas Robinson <NRo...@demon.com> wrote:

>>You don't actually free the button, the form will free it when it
>>destroys itself.

>>Just I'd let you know.

>Nicholas is of course correct.  I have just gotten so paranoid about
>freeing Delphi objects that my rule is to Free anything I Create.

Thomas, I was doing the same thing.  You just can't be too sure.  If you
created a local variable, for instance a TButton, then at the endof the
procedure the 4 byte variable would be destroyed yet the memory for the
object would not.

Its just handy that the form destroys everything it owns, even windows
sometimes, but thats another story!
--
Nicholas Robinson

Other Threads