winforms 如何在Visual Basic中检测到哪个动态图像?

pb3skfrl  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(153)

我一直在做一个饼干点击器风格的游戏,我正在实现一个升级系统的排序。(用一个每秒一次的计时器检测),它调用updateButtons。(working)并使用其ID号调用createImage。它创建一个图像,并为它分配ID的正确图片。最后,它会将它添加到ScreenBox_Clicked的处理程序中,以便在任何升级图像被按下时使用。2我怎样才能知道哪个图片框被按下呢?3这是我的代码。

Public Sub createImage(ByVal imageName)
    Dim tempImage = New System.Windows.Forms.PictureBox() 'Creates New image
    tempImage.Name = "IMG" & imageName.ToString 'Names the image IMG1 or IMG2
    tempImage.BackColor = System.Drawing.Color.Transparent
    tempImage.AutoSize = True
    If imageName = 1 Then 'If the image ID is 1, set it to Image 1's picture tile, if its 2, set it to image 2's picture tile, etc.
        tempImage.BackgroundImage = Global.multiFormsTest.My.Resources.Resources.tile0
    ElseIf imageName = 2 Then
        tempImage.BackgroundImage = Global.multiFormsTest.My.Resources.Resources.tile1
    End If
    tempImage.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
    tempImage.Size = New Size(48, 50)
    tempImage.Location = New Point(1600 + (imageName * 48), 200)
    tempImage.BringToFront()
    AddHandler tempImage.Click, AddressOf ScreenBox_Clicked 'Adds the handler for clicking it to ScreenBox_Clicked.
    Me.Controls.Add(tempImage) 'Adds controls for it.
End Sub '1 = Clicker1
Public Sub updateButtons() 'Called every second
    If (GlobalVariables.totalCookies >= 25 Then
        createImage(1) 'MORE CODE THAT I HAVE DELETED FOR EASY READING (not useful for this problem)
End Sub
Private Sub ScreenBox_Clicked(sender As Object, e As EventArgs)
    'How can I detect whether it was picture box 1, or picture box 2 pressed?
End Sub
rqdpfwrv

rqdpfwrv1#

我看到的是:

Private Sub ScreenBox_Clicked(sender As Object, e As EventArgs)
    'How can I detect whether it was picture box 1, or picture box 2 pressed?
End Sub

sender是按下的方块。如果使用者按一下图像方块1,则sender会参照图像方块1。如果使用者按一下图像方块2,则sender会参照图像方块2。
另外,您应该打开Option Strict。关闭它的功能仍然可以用来维护与旧的.Net之前的代码的兼容性,但是对于新的工作**,您应该使用Option Strict或Option Infer**。

9cbw7uwe

9cbw7uwe2#

If "IMG1" = sender.Name Then
        MsgBox("IMG1")
    ElseIf (sender.Name = "IMG2") Then
        MsgBox("IMG2")
    Else
        MsgBox("error")
    End If

这是我找到的解决方案。请随意评论任何更好的,但这是我找到的,它的工作!

相关问题