6

I have one button on my form. Following is the click event of that button

procedure Form1.btnOKClick(Sender: TObject);
begin
//Do something
end;

This event will be called only when I click the button, right?

How can I call this event automatically without any user intervention?

  • 7
    See [Why is it bad practice to call an eventhandler from code?](http://stackoverflow.com/q/956255/33732), which includes a description of three ways of manually triggering event handlers. – Rob Kennedy Jan 03 '13 at 13:22

3 Answers3

20

The best way to invoke the OnClick event handler attached to a control is to call the Click method on the control. Like this:

btnOK.Click;

Calling the event handler directly forces you to supply the Sender parameter. Calling the Click method gets the control to do all the work. The implementation of the windows message handler for a button click calls the Click method.

But I second the opinion expressed in whosrdaddy's answer. You should pull out the logic behind the button into a separate method.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    +1 This is the only answer that fits perfect the question. OP question is about **events** and **not methods**. Maybe OP did not know the difference, but thats the way he asked :o) – Sir Rufo Jan 03 '13 at 13:30
  • It works for click on buttons but there is not such method on Panel for example. Also what about Resize event? How can I call a Resize event programmatically? – Delmo May 30 '18 at 16:05
  • @Delmo Either call the event handler directly, or extract it into another method that you can call directly, and which the event handler calls. – David Heffernan May 30 '18 at 16:59
  • Thanks @DavidHeffernan, I supposed this kind of solutions but I was asking for some method to "simulate" an event ocurrence. – Delmo May 30 '18 at 21:38
  • As @SirRufo said, the question is about events, not methods. – Delmo May 30 '18 at 21:38
  • @Delmo How else is the event handler going to be fired? – David Heffernan May 30 '18 at 21:58
  • No longer allowed in 10.3 I get error can not access protected symbole , what is the alternative ? – zac Apr 10 '19 at 01:04
  • @wei see final paragraph – David Heffernan Apr 10 '19 at 06:21
17

Do not put your businesslogic into event handlers. This will make your code unreadable when the application grows larger.

Normally you would do this:

procedure TForm1.DoSomething;
begin
 // do something
end;

procedure TForm1.btnOKClick(Sender: TObject);
begin
 DoSomething;
end;

then all you need to do is call DoSomething from other parts in your code

whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
  • Same effect as @LURDs answer, only cleaner code. You will thank yourself later if you do it this way, especially if `// do something` is moved to a separate unit, datamodule, business object. – GolezTrol Jan 03 '13 at 12:29
  • @whosrdaddy - you are right. I am going to do like that. I asked this question only because I wanted to know the second way. –  Jan 03 '13 at 12:30
  • 4
    @GolezTrol: I am re-factoring a 100k LOC behemoth that was written this way, the guy before me needs to be punished :) – whosrdaddy Jan 03 '13 at 12:30
  • Or wrap all the business logic in its own object structure, not related to any GUI. – Jerry Dodge Jan 03 '13 at 21:20
  • @JerryDodge: Yes that's something one would normally do :) – whosrdaddy Jan 03 '13 at 21:28
8

You can call this event in code like any other method.

...
btnOkClick(Self.btnOk); // Sender in this case is the btnOk
...

The Sender can be whatever object you like or nil.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 6
    you did not call the event, you simply call a method. you can't call an event, you can only call the method that is related to an event. – Sir Rufo Jan 03 '13 at 13:34