Recent

Author Topic: programmatically assign event during runtime in lazarus  (Read 18064 times)

bayparinc

  • New Member
  • *
  • Posts: 16
programmatically assign event during runtime in lazarus
« on: June 09, 2009, 03:15:27 am »
In delphi they can do this at runtime, create components and assign its events like

    button1 := TButton.Create(nil);

    //assign the caption
    button1.Caption := 'Button1';
    button1.Height := 45;
    button1.Width := 55;

    button1.Left := Form1.Width - button1.Width);
    button1.Top := Form1.Height - button1.Height);

    //if no parent is set, button will not be visible
    button1.Parent := Form1;

    //assign the OnClick event handler
    button1.OnClick := Button1Click;

While in lazarus this is not possible, it generates a compiler's error of
Error: Wrong number of parameters specified for call to "BitBtn1Click"

Is there a way that we can create components and assign events during runtime in Lazarus?

Thanks.


bobo

  • Full Member
  • ***
  • Posts: 171
Re: programmatically assign event during runtime in lazarus
« Reply #1 on: June 09, 2009, 03:24:07 am »
  button1.OnClick := @Button1Click;

bayparinc

  • New Member
  • *
  • Posts: 16
Re: programmatically assign event during runtime in lazarus
« Reply #2 on: June 09, 2009, 04:03:50 am »
wow, thank you. Problem solved ;)

allanregistos

  • Jr. Member
  • **
  • Posts: 55
  • In Christ Alone
Re: programmatically assign event during runtime in lazarus
« Reply #3 on: July 04, 2016, 07:30:29 am »
  button1.OnClick := @Button1Click;

This topic is old, but I do not want to create a new topic.

What if another control such as a stringgrid with an event : OnSelectCell,
Calling
Code: Pascal  [Select][+][-]
  1. StringGrid2.OnSelectCell := @StringGrid1SelectCell
have this error message:
mainui.pas(181,23) Error: Incompatible types: got "<procedure variable type of procedure(TObject;LongInt;LongInt;var Boolean) of object;Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"
God is my refuge and my strength.

allanregistos

  • Jr. Member
  • **
  • Posts: 55
  • In Christ Alone
Re: programmatically assign event during runtime in lazarus
« Reply #4 on: July 04, 2016, 07:50:34 am »
Okay I've got it. I just copied the OnSelectCell event from StringGrid1 then call it from the created StringGrid:
WorkSheet.OnSelectCell:= @AnotherStringGridSelecCellEvent;
God is my refuge and my strength.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: programmatically assign event during runtime in lazarus
« Reply #5 on: July 04, 2016, 01:18:10 pm »
Just type
Code: [Select]
    StringGrid2.OnSelectCell := @StringGrid1SelectCell

Then set cursor on StringGrid1SelectCell and press Shift+Ctrl+C.
The IDE will create TForm1.StringGrid1SelectCell for you.

Bart

 

TinyPortal © 2005-2018