如何模拟按下按钮? [英] How can I simulate the press of a button?

查看: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 with it?

推荐答案

最干净的方法是调用按钮的Click方法.由于以下原因,这比替代方案更好:

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

  • 您可以读取 OnClick 属性,检查它是否为 nil,然后调用该方法.但这似乎毫无意义,因为 Click 方法已经做到了这一点.复制这段代码没有意义.
  • 您可以直接调用事件处理程序,但这需要您的代码了解它.这意味着与实现细节的耦合程度不理想.
  • 调用 Click 会复制用户单击时实际发生的情况.它当用户按下按钮时发生的事情.例如,它处理与按钮关联的任何操作.它设置表单 ModalResult 属性.等等.
  • 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.

这篇关于如何模拟按下按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆