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

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

问题描述

我正在为我的视觉基础课程做一个游戏。我有多个图片框,单击时将单独显示隐藏的图像。游戏的要点是找到匹配的图片(很简单)。



在最简单的层面上,我有16个图片框。



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

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

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

  pictureBox1.Image =(My.Resources.picture_name)

我想知道是否有办法让一个子句柄全部按下按钮,并更改相应的图片框,而不是有16个单独的处理程序。例如:

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

并做以下:

 '更改相应的图片框

这是什么样子(现在):

解决方案

要查找哪个PictureBox被点击,您只需要查看发件人变量。显然,你必须将它从Object类型转换为PictureBox类型:

  Dim ClickedBox As PictureBox 

ClickedBox = CType(发件人,PictureBox)


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).

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

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)

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

And do the following:

' Change appropriate picture box

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

解决方案

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天全站免登陆