How to handle multiple click events with same Sub

廉价感情. 提交于 2019-11-27 07:04:25

问题


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


回答1:


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)



回答2:


Personally what I would do would be to attach your common EventHandler to your PictureBox, give each PictureBox a Tag for an index, unless you want to do your selection on the name. Then you do something like this.

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



回答3:


According to what I've read, DirectCast is preferred over CType

DirectCast can be combined with 'With/End With' as shown here:

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



回答4:


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


来源:https://stackoverflow.com/questions/13323397/how-to-handle-multiple-click-events-with-same-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!