|
-
May 6th, 2012, 02:58 PM
#1
Thread Starter
Addicted Member
[RESOLVED] dynamic pictureboxes
hello, I create multiple dynamic pictureboxes and it are to many to add them all manually, is there a way I can e.g. use an integer which adds up every time a new picturebox is made. Then I want to add the same code to every picturebox. Now it creates the picturebox and moves it to the next spot and doesn't create a second. I am using this code if its helpfull:
Code I'm using Code:
Dim newimg As PictureBox = New PictureBox
If imgCounter < 5 Then
newimg.Height = 65
newimg.Width = 50
newimg.Name = "img" & imgCounter
newimg.Left = 'i want this to be 100 further every time
newimg.Top = wp1.Location.Y
newimg.Image = My.Resources.img
newimg.Visible = True
newimg.BackgroundImage = My.Resources.Road
newimg.SizeMode = PictureBoxSizeMode.StretchImage
Me.Controls.Add(newimg)
newimg.BringToFront()
imgCounter += 1
call TheCodeSub()
Else
imgSender.Stop() ' timer
imgCounter = 0
End If
-
May 6th, 2012, 03:48 PM
#2
Re: dynamic pictureboxes
I don't think that I quite understand you, however anytime you want to do an action over and over again, then use a loop. Something like this:
Code:
'Variables
Dim img As PictureBox
Dim imgCount As Integer = 0
Dim locate As New Point(0, 0)
Do Until imgCount = 5
'The picturebox's properties
img = New PictureBox
img.Image = My.Resources.Image1
img.Size = New Size(50, 50)
img.SizeMode = PictureBoxSizeMode.Normal
img.Location = locate
'Adding 100 to the Locate.X
locate = New Point(locate.X + 100, locate.Y)
'Adding the picturebox to the form
Me.Controls.Add(img)
'Adding 1 to the integer to keep count of how many pictureboxes I've created
imgCount += 1
Loop
Last edited by dday9; May 6th, 2012 at 03:49 PM.
Reason: Added comments
-
May 7th, 2012, 10:04 AM
#3
Thread Starter
Addicted Member
Re: dynamic pictureboxes
I just want to know how to make code which applys to one of the pictureboxes specifically, it would really help me out, thanks!
Because it seems impossible to use the name since it doesn't exist yet
Last edited by ikdekker; May 7th, 2012 at 10:08 AM.
-
May 7th, 2012, 10:41 AM
#4
Re: dynamic pictureboxes
Ahh, ok I think that I understand you. Like when you add a button to a form and you double click on it, it brings up the button's click event. If that's the case then you want to add an event handler to the picturebox. Take a look here
-
May 7th, 2012, 12:35 PM
#5
Thread Starter
Addicted Member
Re: dynamic pictureboxes
you cant assign code to a specific picturebox that way, because they don't exist yet and the method their using has pictureboxes already in the form.
-
May 7th, 2012, 12:39 PM
#6
Re: dynamic pictureboxes
Yeah you can. The sender argument of the event handler holds the object that raised the event. Therefore, in an event raised by a picturebox, it holds the picturebox. Therefore, you can do this:
Dim pb = DirectCast(sender,PictureBox)
inside the event handler, and now you have the PictureBox that raised the event. You can then do different things based on pb.Name so that different pictureboxes do different things.
My usual boring signature: Nothing
 
-
May 7th, 2012, 01:15 PM
#7
Thread Starter
Addicted Member
Re: dynamic pictureboxes
I get that, but then your adding them manually, right? and I create too many pictureboxes to do it manually, so how would that be done?
-
May 7th, 2012, 01:17 PM
#8
Re: dynamic pictureboxes
Adding what manually? You would have to add a single event handler method and use AddHandler to hook it to each picturebox, but that's no big deal. You are already setting the properties of the picturebox, and it would only be one more line to add the handler.
My usual boring signature: Nothing
 
-
May 7th, 2012, 02:06 PM
#9
Thread Starter
Addicted Member
Re: dynamic pictureboxes
Then could you give me an example, if i create 2 pictureboxes like this. How would I make it work
vb Code:
Dim newimg As PictureBox = New PictureBox dim locate as new point(0,0) If imgCounter < 2 Then newimg = new picturebox newimg.Height = 65 newimg.Width = 50 newimg.Name = "img" & imgCounter newimg.Left = locate.X newimg.Top = wp1.Location.Y newimg.Image = My.Resources.img newimg.Visible = True newimg.BackgroundImage = My.Resources.Road newimg.SizeMode = PictureBoxSizeMode.StretchImage locate = new point(locate.x + 100, locate.y) Me.Controls.Add(newimg) newimg.BringToFront() imgCounter += 1 call TheCodeSub() Else imgSender.Stop() ' timer imgCounter = 0 End If
Now I want to assign e.g. this
vb Code:
AddHandler img1.'if it is created i want the code to execute', AddressOf EventHandler AddHandler img2.'Also on create', AddressOf EventHandler Sub EventHandler() ' some code MsgBox("example") End Sub
Last edited by ikdekker; May 7th, 2012 at 02:11 PM.
-
May 7th, 2012, 02:31 PM
#10
Re: dynamic pictureboxes
The event handler would look like this:
Public Sub Whatever(sender As Object, e As EventArgs)
The actual type for e may not be EventArgs. The easiest way to figure out what you want is to create one event handler for whatever event the normal way, that will give you the proper signature, though the name of the method is unimportant. Only the arguments matter.
You would then add a handler with:
AddHandler img1.Click AddressOf Whatever
Of course, you would use whatever event you wanted, not necessarily Click.
My usual boring signature: Nothing
 
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
|