Home > Blockchain >  How do detect which dynamic image is detected in Visual Basic?
How do detect which dynamic image is detected in Visual Basic?

Time:10-09

I have been making a cookie-clicker style of game, and I am implementing an upgrade system of sorts. When you get to a high enough number of cookies (detected with a timer that goes once per second), it calls updateButtons. Here, it updates the code for prices (working)and calls createImage with its ID number. It creates an image, and assigns it the proper picture for the ID. Finally, it adds it to the handler of ScreenBox_Clicked for when any upgrade image is pressed. How can I tell which picture box was pressed? Heres my code.

 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

CodePudding user response:

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

This was the solution that I found. Feel free to comment anything better, but this is what I found, and it worked!

CodePudding user response:

I see this:

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

The sender is the box that was pressed. If the user click picture box 1, then sender will refer to picture box 1. If they click picture box 2, then sender will refer to picture box 2.

Additionally, you should turn on Option Strict. The ability to have it off is still available to maintain compatibility with older pre-.Net code, but for new work you are expected to use either Option Strict or Option Infer.

  • Related