VB.net:如何在本机控件上引发鼠标单击/键盘事件 [英] VB.net: How to raise mouse-click / keyboard events on native controls

查看:94
本文介绍了VB.net:如何在本机控件上引发鼠标单击/键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景
我想在我的项目中自动测试 Windows 窗体类.我想进行诸如如果单击按钮 F5,是否执行了 xyz 功能?"之类的测试.

Background
I'd like to auto test the Windows Form classes in my project. I'd like to conduct tests like "If Button F5 was clicked, has functionality xyz been executed?".

为了做到这一点,我需要触发鼠标点击或键盘事件.

In order to do that I need to trigger mouse-click or keyboard events.

环境

  • GUI:Windows 窗体
  • 测试框架:MSTests
  • 测试风格:我不测试独立的exe文件

问题
如何在控件上触发鼠标点击或键盘事件?

Question
How can I trigger mouse-click or keyboard events on a control?

+α:如何在任意控件上触发任意事件?

+α: How to trigger any arbitrary event on an arbitrary control?

具体例子
我的生产形式中有一个 GridDataView.在我的测试中,我运行 view.Row (0).selected = True 来选择第一行.现在我想触发一个双击事件和一个 Keys.Enter Key 事件来模拟这些用户交互.

Concrete example
I have a GridDataView in my productive form. In my test I run view.Row (0).selected = True to select the first row. Now I'd like to trigger a doubleclick event and an Keys.Enter Key event to simulate these user interactions.

到目前为止我尝试过的

  • view.PerformDoubleClick()
  • view.DoubleClick (Nothing,Nothing)
  • view.OnDoubleClick(无)
  • RaiseEvent DoubleClick (Nothing,Nothing)
  • RaiseEvent view.DoubleClick (Nothing,Nothing)

这些方法都不起作用;有些甚至不编译

None of these methods worked; some don't even compile

这篇微软文章表明,我正在尝试的事情可能无法完成

This Microsoft article indicates, that it might be impossible to accomplish, what I'm trying

事件只能从声明它的声明空间引发.因此,一个类不能从任何其他类引发事件,即使是派生自它的类也是如此.

An event can be raised only from the declaration space in which it is declared. Therefore, a class cannot raise events from any other class, even one from which it is derived.

推荐答案

我发现的唯一解决方案是将您想要操作的每个控件(小部件)进行子类化,并使它们的 On*() 方法 public.

The only solution I've found is to Subclass every control (widget) you want to operate on and make their On*() methods public.

下面的代码子类化了 Button 以允许从外部使用 OnClick()OnKeyDown() 方法.

The code below subclasses a Button to allow OnClick() and OnKeyDown() methods to be used from outside.

Imports System.Windows.Forms

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class MyButton
  Inherits Button


  Public Shadows Sub OnClick (e As System.EventArgs)
      MyBase.OnClick (e)
  End Sub

  Public Shadows Sub OnKeyDown (kevent As KeyEventArgs)
      MyBase.OnKeyDown (kevent)
  End Sub

End Class

关于KeyDown的注意事项:

Notices regarding KeyDown:

  • 好:如果 FormKeyPreview 设置为 False<,则从外部调用 OnKeyDown() 不会触发任何事情.这与实际关键事件一致,因此很好.
  • 不好:关键事件不会被转发.
    考虑具有 KeyDown 事件的 FormButton.如果用户按下实际 GUI 上的一个键,事件将到达两个控件/小部件.然而,使用这里描述的方法,事件只会在被调用的对象内触发
  • Good: Calling OnKeyDown() from outside will not trigger anything if the Form's KeyPreview is set to False. That is consistent with actual key events and therefore good.
  • Bad: Key events won't be forwareded.
    Consider a Form and a Button having a KeyDown event. If the user presses a key on the actual GUI, the event will arrive at both controls/widgets. However, using the method described here, the event will only be triggered within the object was was called upon

这篇关于VB.net:如何在本机控件上引发鼠标单击/键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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