如何处理具有相同Sub的多个点击事件 [英] How to handle multiple click events with same 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屋!