Reputation:
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?
Upvotes: 6
Views: 19728
Reputation: 612794
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.
Upvotes: 20
Reputation: 11860
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
Upvotes: 17
Reputation: 34889
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.
Upvotes: 8