Recent

Author Topic: new event  (Read 6591 times)

emil

  • New Member
  • *
  • Posts: 16
new event
« on: March 20, 2015, 06:59:43 pm »
i'm working on a game
there is many panels with many buttons (menu)
i've made an array of panels and buttons
but i need the event - action - when the button is clicked
need something very very simple

help please

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: new event
« Reply #1 on: March 20, 2015, 07:10:23 pm »
select one of your buttons on the form.
in the IDE Object Inspector, click on the Events tab.
here you can click on the 'On Click' event and select a Event Handler (procedure in your code) to perform what you want when the button is clicked by the user.
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: new event
« Reply #2 on: March 20, 2015, 07:11:24 pm »
Two methods:

1) Double click on the button and the OnClick procedure will be completed.
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin

end; 


2) Open the ObjectInspector, select the button at the top, then go to the events tab and click on the ellipsis ... symbol.

emil

  • New Member
  • *
  • Posts: 16
Re: new event
« Reply #3 on: March 20, 2015, 07:35:42 pm »
procedure button[1]Click(sender:TObject);
begin
     with button[1] do
     begin
          onclick:=@button[1]click;
     end;
end;

procedure form1.button[1]Click(sender:TObject);
begin

end;


why it doesnt work?
unit1.pas(230,16) Error: overloaded identifier "buton" isn't a function

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: new event
« Reply #4 on: March 20, 2015, 07:39:57 pm »
You are making it more difficult than it is.
Go to the outline of the new procedure, and just enter your code. Try the example below.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Color := clRed;
  Button1.Caption := 'Success';
end; 

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: new event
« Reply #5 on: March 20, 2015, 07:40:07 pm »
procedure button[1]Click(sender:TObject);
begin
     with button[1] do
     begin
          onclick:=@button[1]click;
     end;
end;

procedure form1.button[1]Click(sender:TObject);
begin

end;


why it doesnt work?
unit1.pas(230,16) Error: overloaded identifier "buton" isn't a function

is button an array? If not then this will never work remove the brackets. Even if it is a array you are not allowed to use brackets in the name of a method so procedure button[1]Click(sender:TObject); is not valid again remove the brackets. when the brackets have been removed from the code then post the exact error message so we can take a look
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9867
  • Debugger - SynEdit - and more
    • wiki
Re: new event
« Reply #6 on: March 20, 2015, 07:46:21 pm »
If you have somewhere
Code: [Select]
  button: array of TButton

then create an event for the forms "OnCreate" method (should happen by double clicking the form)

in there do
1) if you have NOT yet created the buttons
Code: [Select]
  SetLength(button, 25);
  for i := 0 to length(button) do begin
     button[i] := TButton.create(self)
     button[i].parent :- self; // or a panel, if you have
     button[i].top := ....
  end;

2) for the OnClick
Code: [Select]
  for i := 0 to length(button) do begin
     button[i].OnCreate := @MyButtonClickHandleThatINamedWhateverILiked;
  end;

press shift=ctrl-C on the MyButtonClickHandleThatINamedWhateverILiked and the method will be created.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: new event
« Reply #7 on: March 20, 2015, 08:00:27 pm »
procedure button[1]Click(sender:TObject);

1.  Not valid pascal.  You can't have [ or ] in a procedure/function name.
2.  This procedure needs to belong to the Form object.  As it is, any button you try to reference will be unknown within that procedure.

          onclick:=@button[1]click;

Same syntax error as above.

I'm going to assume here you've tried to declare button[1] as a control name.  Not valid pascal syntax.  You may also be assuming that button[1] gives you a button in an array you think the form has.  This isn't the case.  If you have multiple buttons, you will need to create and manage such an array your self.  Of course, you may not be making this mistake and it's just the syntax error that's tripping you up.

why it doesnt work?
unit1.pas(230,16) Error: overloaded identifier "buton" isn't a function

if you typed this by hand and mispelt Buton, then this is how the compiler has attempted to compile your syntax error.  Garbage In, Garbage Out...

IF you have a control named Button1 and you've correctly declared FormCreate and Button1Click, then the following should work...

Code: [Select]
procedure form1.FormCreate(sender:TObject);
begin
     with button1 do
     begin
          onclick:=@button1click;
     end;
end;

procedure form1.button1Click(sender:TObject);
begin

end;
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

emil

  • New Member
  • *
  • Posts: 16
Re: new event
« Reply #8 on: March 21, 2015, 12:05:13 am »
Ok guys
thx for help
specialy to Martin_fr
my code looks like:
...
for i:=1 to 10 do
          begin
              button:=TButton.create(form1);
              button.parent:=panel[1];
              button.enabled:=true;
          end;
...
{from here}
procedure TForm1.MyButtonClickHandle(Sender: TObject);
begin

end;
{to here, i didnt write anything, i CLICKED on MyButtonClickHandle , pressed shift-ctl-c, and it apeared}

procedure TForm1.FormClick(Sender: TObject);
begin
     button[1].Onclick:=@MyButtonClickHandle;
end;

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9867
  • Debugger - SynEdit - and more
    • wiki
Re: new event
« Reply #9 on: March 21, 2015, 12:26:43 am »
procedure TForm1.FormClick(Sender: TObject);
begin
     button[1].Onclick:=@MyButtonClickHandle;
end;

That should be FormCreate Not Click.

Or better, that should be in the same place where you already create your buttons.

---
Quote
{to here, i didnt write anything, i CLICKED on MyButtonClickHandle , pressed shift-ctl-c, and it apeared}
You really want to read this page: http://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools
« Last Edit: March 21, 2015, 12:28:15 am by Martin_fr »

emil

  • New Member
  • *
  • Posts: 16
Re: new event
« Reply #10 on: March 21, 2015, 09:51:25 am »
    button[1].OnCreate:=@BuildForest;
    button[1].FormCreate:=@BuildForest;
    button[1].onFormCreate:=@BuildForest;

doesnt work:

unit1.pas(291,15) Error: identifier idents no member "OnCreate"

same error to all the 3 cases

          for i:=1 to 10 do
          begin
              button:=TButton.create(form1);
              button.parent:=panel[1];
              button.enabled:=false;
              button[1].OnCreate:=@BuildForest;
          end;                       

unit1.pas(282,25) Error: identifier idents no member "OnCreate"


BUT

if i write "onclick" and press ctrl-shift-c, it works properly
« Last Edit: March 21, 2015, 09:59:19 am by emil »

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: new event
« Reply #11 on: March 21, 2015, 10:44:53 am »
@emil
you have your form (A)
you want your buttons (Bs) in (A)
you create the buttons when (A) begins to live ---->inside (A) create event
assign the events for your (Bs) just then ----->inside (A) create event and after your (Bs)  are created
------------------------
create (A)+
                 |--->create(Bs)
                 |--->assign (Bs) events
show(A)
.....
....
click(A)
.....
......other events....

« Last Edit: March 21, 2015, 10:47:23 am by Never »
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: new event
« Reply #12 on: March 21, 2015, 06:14:58 pm »
    button[1].OnCreate:=@BuildForest;

The compiler is not lying to you.  There is no OnCreate event for you to assign to.  To verify, drop a TButton on the form.  Have a look in the Object Inspector.  There is a tab there called Events.  That's the list of events (see attached).

Besides - if you look at your code, there's something wrong in your logic.

1.  You create the button
Code: [Select]
button[i]:=TButton.create(form1);
Two lines later, you attempt to hook to an OnCreate.
Code: [Select]
button[1].OnCreate:=@BuildForest;
(did you mean to use an i in there, not a 1?)

This is too late.  At the TButton has already been created, if the OnCreate event existed (which it doesn't for TButton), then hooking up to it at this point will achieve nothing.  You needed to hook up to it BEFORE the control was created.  Object Oriented code is what you most probably need, but for now, there's an easier way....

Quite simply, as you are the person creating the TButton, you can fire the "OnCreate" yourself...

Code: [Select]
          for i:=1 to 10 do
          begin
              button[i]:=TButton.create(form1);
              button[i].parent:=panel[1];
              button[i].enabled:=false;
              BuildForest(button[i]);
          end;     
   

Finally, please use [ code ] tags instead of [ quote ] tags when posting code.  [ quote ] tags gets confused when you use [ i ], they think you want to start italics ;)  (Don't use spaces in the tags, I added them to trick the forum into displaying the text instead of rendering them as tags...)
« Last Edit: March 21, 2015, 06:18:26 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

ThomasK1201

  • New Member
  • *
  • Posts: 31
Re: new event
« Reply #13 on: March 24, 2015, 09:20:47 pm »
for i:=1 to 10 do
          begin
              button:=TButton.create(form1);
              button.parent:=panel[1];
              button.enabled:=true;
          end;
...
{from here}
procedure TForm1.MyButtonClickHandle(Sender: TObject);
begin

end;
{to here, i didnt write anything, i CLICKED on MyButtonClickHandle , pressed shift-ctl-c, and it apeared}

procedure TForm1.FormClick(Sender: TObject);
begin
     button[1].Onclick:=@MyButtonClickHandle;
end;

One very useful tip (or at least I found it useful when it was told to me):
Use more spaces. So:
Code: [Select]
for i:=1 to 10 do
          begin
              button[i]:=TButton.create(form1);
              button[i].parent:=panel[1];
              button[i].enabled:=true;
          end;

will become:

Code: [Select]
for i:=1 to 10 do
          begin
              button[i] := TButton.create(form1);
              button[i].parent := panel[1];
              button[i].enabled := true;
          end;
So first Button.Enabled. Then add a space. Then add := , then add a space and then True. Button.Enabled := True;

 

TinyPortal © 2005-2018