Results 1 to 10 of 10

Thread: [RESOLVED] dynamic pictureboxes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Resolved [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:
    1. Dim newimg As PictureBox = New PictureBox
    2.         If imgCounter < 5 Then
    3.             newimg.Height = 65
    4.             newimg.Width = 50
    5.             newimg.Name = "img" & imgCounter
    6.             newimg.Left = 'i want this to be 100 further every time
    7.             newimg.Top = wp1.Location.Y
    8.             newimg.Image = My.Resources.img
    9.             newimg.Visible = True
    10.             newimg.BackgroundImage = My.Resources.Road
    11.             newimg.SizeMode = PictureBoxSizeMode.StretchImage
    12.            
    13.  
    14.  
    15.             Me.Controls.Add(newimg)
    16.             newimg.BringToFront()
    17.             imgCounter += 1
    18.             call TheCodeSub()
    19.         Else
    20.             imgSender.Stop() ' timer
    21.             imgCounter = 0
    22.         End If

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,383

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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:
    1. Dim newimg As PictureBox = New PictureBox
    2. dim locate as new point(0,0)
    3.         If imgCounter < 2 Then
    4.             newimg = new picturebox
    5.             newimg.Height = 65
    6.             newimg.Width = 50
    7.             newimg.Name = "img" & imgCounter
    8.             newimg.Left = locate.X
    9.             newimg.Top = wp1.Location.Y
    10.             newimg.Image = My.Resources.img
    11.             newimg.Visible = True
    12.             newimg.BackgroundImage = My.Resources.Road
    13.             newimg.SizeMode = PictureBoxSizeMode.StretchImage
    14.            
    15.  
    16. locate = new point(locate.x + 100, locate.y)
    17.             Me.Controls.Add(newimg)
    18.             newimg.BringToFront()
    19.             imgCounter += 1
    20.             call TheCodeSub()
    21.         Else
    22.             imgSender.Stop() ' timer
    23.             imgCounter = 0
    24.         End If
    Now I want to assign e.g. this
    vb Code:
    1. AddHandler img1.'if it is created i want the code to execute', AddressOf EventHandler
    2. AddHandler img2.'Also on create', AddressOf EventHandler
    3.  
    4. Sub EventHandler()
    5.     ' some code
    6.     MsgBox("example")
    7. End Sub
    Last edited by ikdekker; May 7th, 2012 at 02:11 PM.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width