Results 1 to 15 of 15

Thread: Picturebox Arrays?

  1. #1
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0

    Picturebox Arrays?

    Hi All
    I have a quick question regarding multiple picture boxes.

    I am trying to design a basic version of spaceinvaders. I have 6 pictureboxes on my form and the idea is to add a value to the picbox.top property that moves the boxes down at a certain time and speed that i have set in my timer control.

    The problem i have is i am trying to set properties for all the pictureboxes at the same time by using arrays.

    An example would be

    Code:
    dim mypic() as picturebox = {pic1, pic2, pic3, pic4, pic5, pic6}
    pic1.top = 10
    pic2.top = 10
    'and so on


    I am unable to alter the top property collectively for all the boxes. it works fine for each individual box.

    Can anyone tell me how I would go about that.
    Also when it comes to chaging the top property, my code snippet in my timer_tick even would include

    Code:
    static a as integer
    a = pic1.top
    
    a += 1
    
    pic1.top = a
    
    exit sub
    The above works fine and would move pic1 down the form everytime the timer ticks (interval is set at 500 milliseconds).

    I want to be able to move all picboxes down the form by using the array method as supposed to individually and carry on with any other relevant coding from there.

    I hope the above makes sense but please contact me if you need to clarify any of the above.

  2. #2
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    If you store then in a List(Of T) you can add and remove them as you need. Better than an array.
    Code:
    Dim mypics As New List(Of Picturebox)
    
    'add the PB's to the List
    mypics.Add(pic1)...
    
    For Each pb As PictureBox In mypics
      pb.Top += 1
    Next
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  3. #3
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    Actually I don't think .Net 1.1 will support List(Of T).
    Any chance you could upgrade 3.5+ is where you need to be.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  4. #4
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0
    i think i am already on 3.5> the List(of T) did work as far as adding each picbox to the list. However just looking at the code it is looking like i will have to use a variable for each picbox.top integer to add a value in order that the pic box moves to the bottom. THis brings me back to using individual codes for each box. Isnt this the whole point of using arrays.

  5. #5
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    In my code I have iterated the List to find each of the pb's in the List so all are moving not just 1. So did you even try it?
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  6. #6
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0
    sorry should have mentioned earlier. I did try using your code but had to make a slight adjustment to it by using a variable for the pb.top value (see below)



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Dim mypics As New List(Of PictureBox)
    Static a As Integer


    mypics.Add(pic1)
    mypics.Add(pic2)
    mypics.Add(pic3)
    mypics.Add(pic4)
    mypics.Add(pic5)
    mypics.Add(pic6)
    For Each pb As PictureBox In mypics
    If pb.Bottom > 0 Then
    pb.Top = a
    a += 2

    Else
    a = Nothing
    Exit Sub
    End If
    Next

    End Sub
    End Class
    My pic boxes are now all moving when the timer ticks. I am working on getting them to stop when they reach the bottom and to reset back to the top. When a user double clicks on them (using the sender object) or when they reach the bottom(or the escape line).

    Thank you for your help newguy> I guess my only question would be when a value is added to each top variable, why are my pic boxes not moving or descending in a straight line?

  7. #7
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    Here is a short cut:
    Code:
    mypics.AddRange(New PictureBox() {pic1, pic2, pic3, pic4, pic5, pic6})
    You need a Do/Loop for moving them with a condition to exit the loop or redirect them.

    Read this post - #3 has some logic you could use.
    Last edited by newguy; 12-23-2009 at 4:01 PM.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  8. #8
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0
    Hi NewGuy
    Hope you had a good Christmas break. Not sure if you are still looking at this post but if you are (or anybody else is), would you know why the following code is only moving one box as supposed to all the picturebox controls on the form. I have 2 versions of the code based on your recommendation. Version B moves all boxes (but i would rather not use the if..then clause) and Version A only moves one box ie pic1.

    See below

    Version A
    Code:
    Public Class Form1
        'Dim MyPics As Array = New PictureBox() {pic1, pic2, pic3, pic4, pic5, pic6}
        Dim mypic As New List(Of PictureBox)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If Timer1.Enabled = False Then
                Timer1.Enabled = True
                Timer1.Interval = 500
    
            Else
                Timer1.Enabled = False
            End If
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            mypic.AddRange(New PictureBox() {pic1, pic2, pic3, pic4, pic5, pic6})
            Static a As Integer = 10
            Dim b As Integer = 593
    
            For Each pb As PictureBox In mypic
                While a < b
                    pb.Top = a
                    a += 10
                    Exit Sub
                End While
            Next
            a = Nothing
        End Sub
    End Class
    Version B

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If Timer1.Enabled = False Then
                Timer1.Enabled = True
                Timer1.Interval = 500
    
            Else
                Timer1.Enabled = False
            End If
            ''pic1.Top = 10
    
        End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim mypics As New List(Of PictureBox)
            Static a As Integer
            Dim b As Integer = 10
            mypics.AddRange(New PictureBox() {pic1, pic2, pic3, pic4, pic5, pic6})
    
    
            For Each pb As PictureBox In mypics
                If pb.Bottom < 539 Then
                    pb.Top = a
                    a += 2
    
                Else
                    pb.Top = 10
                    a = Nothing
                    pb.Top = a
                    a += 2
                    Exit Sub
                End If
            Next
    
        End Sub
    Last edited by tt1611; 12-30-2009 at 12:39 PM.

  9. #9
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    1) your code should be in code tags, not the quote tags - better readability,
    2) your re-making the List with every tick event - they probably should be added in the form load event and just the loop in here.
    3) debug your code, when you put the cursor over a variable it will tell you what value it holds, this is very help full.
    4) if your just adding to the top property you don't need to make a new variable:
    pd.Top += 2 does the same thing.
    Hope this helps...
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  10. #10
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162

    You CAN create a control array in VB.NET

    Here is an example using 5 textboxes and a button to prove it works. Adapt the code to use with your own project and the picture boxes:


    Code:
    Public Class Form1
    	Private TextArray(5) As TextBox		'or 4 starting with 0
    
    	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    		Dim count As Integer = 5		'or 4 to go from 0 to 4
    		Dim txt As TextBox
    		For Each ctl As Control In Controls
    			If TypeOf ctl Is TextBox Then
    				txt = CType(ctl, TextBox)
    				'If CType(txt.Tag, Integer) = 1 Then   'Use only if textboxes are Tagged
    				TextArray(count) = txt
    				count -= 1
    				'End If
    			End If
    		Next
    	End Sub
    
    	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    		For x As Integer = 1 To 5		'or 0 to 4
    			'will also work with 0 To 5 if 6 textboxes used
    			TextArray(x).Text = x.ToString	'or TextArray(x).Text = (x + 1).ToString
    		Next
    	End Sub
    
    End Class

  11. #11
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0
    your code should be in code tags, not the quote tags - better readability,
    not sure what was going on with me but i have corrected this.


    2) your re-making the List with every tick event - they probably should be added in the form load event and just the loop in here.
    3) debug your code, when you put the cursor over a variable it will tell you what value it holds, this is very help full.
    As far as both of the above goes, if i dont remake the list, everytime the tick counts, as i noticed when i debugged, the count of my picturebox list kept growing exponentially from 6 to 12 to 18 and so on.

    I have checked every crack of this code while debugging. It all seems to be worded right but for some reason it is still only moving the first picture box in the list. I have moved my declaration
    Code:
    Dim mypic As New List(Of PictureBox)
    to the top as a global declaration, populated the list in the form load event and left just the loop in the timer control and still only able to move one box at a time.

    Guess i will try and figure something out or probably just resort back to if...then

    as far as pb+=2 goes, i need a variable declared static to hold the current value when the timer ticked hence i decided to assign a variable "a" holding the current top value that didnt change till the pciture reached the bottom of the frame.

  12. #12
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    250
    It will not keep adding the PBs, your simply iterating the list in the loop. The only way it would keep adding them is if you tell it too - the AddRange thing.

    The top property also remembers the value it is changed to. So if ask for the top property and does it = something then add some more to it and it will change to this new value.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  13. #13
    tt1611 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1
    Join Date
    Nov 2009
    Posts
    9
    Reputation
    0
    Thanks for the above code Solitaire, I can see the above code if i'm not mistaken counts the total amount of textboxes in the form and displays results accordingly. My project is trying to manipulate all controls of type picturebox in an array at the same time and move them according to existing code.

    I guess in your above code, my a similar question would be how do you write the same information to all textboxes in a the same array ie TextArray(5).

  14. #14
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162
    In the button Click event:

    For x As Integer = 1 To 5
    TextArray(x).Text = "Information"
    Next

  15. #15
    rcombs4 is offline VB.NET Forum Genius
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2008
    Posts
    188
    Reputation
    192
    FYI this thread is really old.

    Also, the reason its only moving the first picture box is because you have an 'Exit Sub' in the loop.
    --Insert Clever Signature--

Tags for this Thread

Bookmarks

Posting Permissions

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