vb.net 如何使用同一个 Sub 处理多个点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13323397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle multiple click events with same Sub
提问by Gingerbeard
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.
在最简单的层面上,我有 16 个图片框。图片框的数量随着难度的增加而增加。
For each picture box, I currently have an event handler as follows (default created by visual studio):
对于每个图片框,我目前有一个如下的事件处理程序(默认由 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:
我想知道是否有一种方法可以让一个 Sub 处理所有按钮点击,并更改适当的图片框,而不是有 16 个单独的处理程序。例如:
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):
这是它的样子(目前):
回答by ZippyV
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:
要找出点击了哪个 PictureBox,您只需查看 sender 变量。显然你必须将它从 Object 类型转换为 PictureBox 类型:
Dim ClickedBox As PictureBox
ClickedBox = CType(sender, PictureBox)
回答by Mark Hall
Personally what I would do would be to attach your common EventHandler to your PictureBox, give each PictureBox a Tagfor an index, unless you want to do your selection on the name. Then you do something like this.
就我个人而言,我会做的是将您的公共 EventHandler 附加到您的 PictureBox,给每个 PictureBox 一个索引标签,除非您想对名称进行选择。然后你做这样的事情。
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click, ...
Dim pb As PictureBox = CType(sender, PictureBox)
Select Case CInt(pb.Tag)
Case 0
pb.Image = My.Resources.PictureName1
Case 1
pb.Image = My.Resources.PictureName2
...
End Select
End Sub
回答by FJones
According to what I've read, DirectCast is preferred over CType
根据我所读到的内容,DirectCast 优于 CType
DirectCast can be combined with 'With/End With' as shown here:
DirectCast 可以与“With/End With”结合使用,如下所示:
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click, ...
With DirectCast(sender, PictureBox)
Select Case CInt(.Tag)
Case 0
.Image = My.Resources.PictureName1
Case 1
.Image = My.Resources.PictureName2
...
End Select
End With
End Sub
I've tried the following also but this causes strange problems (controls disappearing).
我也尝试了以下方法,但这会导致奇怪的问题(控件消失)。
Using cbMe as CheckBox = DirectCast(sender, CheckBox)
cbMe.Checked = True
End Using
回答by Mamoon Ahmed
Iterate through all controls for example
例如,遍历所有控件
For Each ctr As Control In Me.Controls
If TypeOf ctr Is PictureBox Then
If ctr Is ActiveControl Then
' Do Something here
End If
End If
Next