如何处理具有相同 Sub 的多个点击事件 [英] How to handle multiple click events with same Sub

查看:17
本文介绍了如何处理具有相同 Sub 的多个点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的视觉基础课程制作游戏.我有多个图片框,单击它们会分别显示隐藏的图像.游戏的重点是找到匹配的图片(很简单).

I'm making a game for my visual basic course. I have multiple picture boxes that when clicked will reveal a hidden image individually. The point of the game is to find the matching pictures (simple enough).

在最简单的级别上,我有 16 个图片框.图片框的数量随着难度的增加而增加.

On the easiest level, I have 16 picture boxes. The number of picture boxes increases as the difficulty increases.

对于每个图片框,我目前有一个事件处理程序如下(默认由visual studio创建):

For each picture box, I currently have an event handler as follows (default created by visual studio):

Private Sub pictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pictureBox1.Click

里面,我打算用这个来改变图片框中的图片,如下:

Inside, I plan to use this to change the image in the picture box, as follows:

pictureBox1.Image = (My.Resources.picture_name)

我想知道是否有办法让一个 Sub 处理所有按钮点击,并更改适当的图片框,而不是使用 16 个单独的处理程序.例如:

I would like to know if there is a way to have one Sub handle ALL the button clicks, and change the appropriate picture box, instead of having 16 separate handlers. For example:

Private Sub pictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
     Handles pictureBox1.Click, pictureBox2.Click, pictureBox3.Click, ... pictureBox16.Click

然后执行以下操作:

' Change appropriate picture box

这是它的样子(目前):

Here's what it looks like (for now):

推荐答案

要找出点击了哪个 PictureBox,您只需查看 sender 变量.显然你必须将它从 Object 类型转换为 PictureBox 类型:

To find out which PictureBox was clicked you just have to look at the sender variable. Obviously you have to convert it from the Object type to the PictureBox type:

Dim ClickedBox As PictureBox

ClickedBox = CType(sender, PictureBox)

这篇关于如何处理具有相同 Sub 的多个点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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