Board index » delphi » "Runtime created button"onclick event linking

"Runtime created button"onclick event linking

Hello,
After spending a whole day on that problem, i would like to submit you
a little question.

On the principal form (Form1) is a button (AddOneGraph). The code
associated with the onclick event of this button is given hereafter.
Briefly described, this button must create and display a form
(newform) containing a graph (Graphe) and a button (Bubu). Till here,
no problem.

The goal of the Bubu button should be to close the form just created
(newform, thus) by clicking on it instead of to have to click on the
smaal cross in the upper-right corner.

Here is the problem: with the code below, the form display but
immediately closes.
It do not wait till an onclick event occurs! And I also cannot use the
show modal method to display it, because i need to create a serie of
graph. I already tried it (iow the line "newform.show" replaced by
"newform.showmodal", and after the first displayed, no more form were
possible to create.

So, how to make the BubuClick procedure occuring only if the bubu
button is really clicked? I thing i missed something to link the event
and a caal to the procedure.

Should something as: if  Bubu.Onclick = true then...

Thank you for helping a poor beginner!
JBF.

Here is my code:

procedure TForm1.AddOneGraphClick(Sender: TObject);
var NewForm : Tform;
    Graphe : Tchart;
    Bubu : TButton;
    procedure BubuClick(Sender:TNotifyEvent);
         begin
         NewForm.Close;
         end;
{+++ Here starts the procedure +++}
begin
{Form creation and setting}
NewForm := TForm.Create(Application);
{Graph creation and setting}
Graphe:=Tchart.Create(NewForm);
Graphe.Parent:=NewForm;
{Button creation and setting}
Bubu:=TButton.Create(NewForm);
Bubu.Parent:=NewForm;
Bubu.Caption:='Click me';
{Display}
NewForm.Show;
{Closing}
BubuClick(Bubu.Onclick );
end;

 

Re:"Runtime created button"onclick event linking


JB FALLON,

Quote
>procedure TForm1.AddOneGraphClick(Sender: TObject);
>var NewForm : Tform;
...
>{Closing}
>BubuClick(Bubu.Onclick );
>end;

The above line "BubuClick(Bubu.Onclick)" is causing the problem, I think.  I
think you're trying to assign BubuClick to Bubu's OnClick handler, which
should be written as:
Bubu.OnClick := BubuClick;

What you are doing in your code is CALLING BubuClick with Onclick as the
Sender!  Hence, you are closing the form immediately after showing it.  Try
my assignment line instead and see if that helps.

Good luck!
Dave

Re:"Runtime created button"onclick event linking


Hi JBF,

Next code seems to do the trick although I have the feeling
this is very dirty programming!

Have fun with it
- Pieter

======================================
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure BubuClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BubuClick(Sender: TObject);
begin
  TForm(TButton(Sender).parent).Release;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  NewForm : TForm;
  Bubu    : TButton;
begin
  NewForm := TForm.Create(Application);

  Bubu := TButton.Create(NewForm);
  Bubu.Parent  := NewForm;
  Bubu.Caption := 'Click me';
  NewForm.Show;
  Bubu.Onclick := BubuClick;
end;
======================================

Other Threads