How can I simulate the press of a button?

前端 未结 5 759
天命终不由人
天命终不由人 2021-01-18 15:25

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

5条回答
  •  無奈伤痛
    2021-01-18 16:22

    The cleanest approach is to call the Click method of the button. This is better than the alternatives for these reasons:

    • You could read the OnClick property, check that it was not nil, and then call the method. But that seems rather pointless since the Click method already does just that. There's no point duplicating this code.
    • And you could call the event handler directly, but that would require your code to have knowledge of it. That implies an undesirable level of coupling to the implementation details.
    • Calling 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.

提交回复
热议问题