|
-
Apr 26th, 2020, 08:20 AM
#1
Thread Starter
New Member
[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
-
Apr 26th, 2020, 08:43 AM
#2
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
-
Apr 26th, 2020, 09:19 AM
#3
Thread Starter
New Member
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?
-
Apr 26th, 2020, 10:04 AM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 26th, 2020, 10:19 AM
#5
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:
Private ReadOnly numbersByPictureBox As New Dictionary(Of PictureBox, Integer) Private Sub CreatePictureBox() Dim pb As New PictureBox '... AddHandler pb.Click, AddressOf PictureBoxes_Click numbersByPictureBox.Add(pb, 0) End Sub Private Sub PictureBoxes_Click(sender As Object, e As EventArgs) Dim pb = DirectCast(sender, PictureBox) numbersByPictureBox(pb) += 1 End Sub
-
Apr 26th, 2020, 02:08 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|