I want to test some forms. Is there a way to simulate the press of an Ok (or Cancel) button so that the button is pressed and fires the event handlers that are associated wi
It's better to use the PerformClick()
method of the Button =>
button1.PerfomClick()
If your Button is not in the right state to click (enabled false or not visible), it will not perform the click eventmethod.
btn_ok.click
or btn_okClick(sender);
The cleanest approach is to call the Click method of the button. This is better than the alternatives for these reasons:
Click
replicates what actually happens when the user clicks. It is what happens when the user presses the button. It deals with any actions that are associated with the button, for example. It sets the forms ModalResult property. And so on.A correction for TSpeedButton:
The behavior described by @David Heffernan does not quite hold true for speedbuttons in a group. Calling the Click method does not seem to affect the "Down" status of the buttons.
To solve this I used the following code:
MyButton.Click;
MyButton.Down := True;
Calling the OnClick
event handler won't call the Delphi's default event handler, but just the one implemented by the user. I think you should create your own class derived from TCustomButton
, and implement a function that calls the Click
method(it is protected).