Results 1 to 6 of 6

Thread: [RESOLVED] Detecting a click on a picturebox generated from an array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    Resolved [RESOLVED] Detecting a click on a picturebox generated from an array?

    Hello, vbforums

    Is it possible for me to detect a click on a picturebox generated from an array? In my code, I have a function that declares new pictureboxes and puts them in a random location. I have another array called boxhp, which is an integer and has the same amount of items in at as the number of pictureboxes. When I click on a picturebox, I want the corresponding boxhp to decrease. For example, when I click on box(6), I want boxhp(6) to decrease in value.

    I cannot create an event handler for every single box, as it would be impractical due to the sheer amount of pictureboxes.
    There is probably something incredibly simple I'm missing here, but oh well

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Detecting a click on a picturebox generated from an array?

    One event can handle a click on all your pictureboxes. Something like this:

    Code:
        Private Sub pb1_Click(sender As Object, e As EventArgs) Handles pb1.Click, pb2.Click, pb3.Click ' etc
    
            Dim pb As PictureBox = DirectCast(sender, PictureBox)
    
            Select Case pb.Name
                Case "pb1"
                    ' Do stuff
                Case "pb2"
                    ' other stuff
                Case "pb3"
                    ' etc
            End Select
    
        End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    Re: Detecting a click on a picturebox generated from an array?

    Thanks for teaching me something new today Paul. However, this isn't the solution I'm looking for.
    This is the code I use to add the pictureboxes to the form:
    Code:
    Dim box(20) as Picturebox
    For i As Integer = 0 To 20
                box(i) = New PictureBox
                box(i).Visible = True
                box(i).BackColor = Color.Green
                box(i).Size = New Size(100, 50)
                box(i).Location = New Point(Rnd() * Width, Rnd() * Height)
                Me.Controls.Add(box(i))
            Next
    I can't use one event handler for all of the pictureboxes because they don't exist yet; they are waiting to be generated in an array. If I put the names of the pictureboxes that will be generated when the program runs, I get an error because they are not existing components yet.
    Code:
    Private Sub PictureboxClick(sender As Object, e As EventArgs) Handles picturebox1.Click
    "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."
    How do I go about detecting clicks on these pictureboxes?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Detecting a click on a picturebox generated from an array?

    Code:
    Dim box(20) as Picturebox
    For i As Integer = 0 To 20
        box(i) = New PictureBox
        box(i).Visible = True
        box(i).BackColor = Color.Green
        box(i).Size = New Size(100, 50)
        box(i).Location = New Point(Rnd() * Width, Rnd() * Height)
        Me.Controls.Add(box(i))
       AddHandler box(i).Click, AddressOf boxes_Click
    Next
    Code:
    Private Sub boxes_Click(sender As Object, e As EventArgs)
    
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
    
        Select Case pb.Name
            Case "pb1"
                ' Do stuff
            Case "pb2"
                ' other stuff
            Case "pb3"
                ' etc
        End Select
    
    End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Detecting a click on a picturebox generated from an array?

    You should create a Dictionary where the PictureBoxes are the keys and the numbers are the values. When you create a PictureBox, add it to the Dictionary with a value of zero. On a click, use the sender as a key into the Dictionary and increment the value.
    vb.net Code:
    1. Private ReadOnly numbersByPictureBox As New Dictionary(Of PictureBox, Integer)
    2.  
    3. Private Sub CreatePictureBox()
    4.     Dim pb As New PictureBox
    5.  
    6.     '...
    7.  
    8.     AddHandler pb.Click, AddressOf PictureBoxes_Click
    9.     numbersByPictureBox.Add(pb, 0)
    10. End Sub
    11.  
    12. Private Sub PictureBoxes_Click(sender As Object, e As EventArgs)
    13.     Dim pb = DirectCast(sender, PictureBox)
    14.  
    15.     numbersByPictureBox(pb) += 1
    16. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Detecting a click on a picturebox generated from an array?

    when I click on box(6), I want boxhp(6) to decrease in value.
    If using jmcihinney's code, then instead of setting the value to 0, you would set it to whatever value you would have set in you boxhp array.
    The dictionary values replace the boxhp array in this case, and you're indexing that "array" by Picturebox, instead of an Integer.

    If you still need the integer index to index into other arrays in your code, then perhaps a bit of reorganization of your code could be done.
    If you put the things that may be in parallel arrays into a class as class members, then you could have an array of that class, so all the related things for a given index can be accessed together.
    The dictionary values would then be an instance of that class, rather than an Integer.

    A variation of what jmcilhinney did, which would fit into your existing code, is to keep track of an index value when you add pictureboxes to the dictionary, and increment that value as you add pictureboxes, and set the dictionary value to that index value, rather than 0.
    The dictionary then becomes a lookup table, to map the picturebox to an index, and you would then use the index to access the other things in your existing code.

    Of course, if you were going to do that, then you wouldn't need a dictionary, as the picturebox already has a .Tag property, and you could set the .Tag to index value when you create the picturebox.
    Then, when you click on the picturebox, you use the .Tag value to index into your boxhp array to decrement the correct entry.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width