Results 1 to 3 of 3

Thread: [RESOLVED] ArrayList returns boolean instead of PictureBox object

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Resolved [RESOLVED] ArrayList returns boolean instead of PictureBox object

    VB Code:
    1. Dim pic2 As PictureBox
    2.                 pic2 = snake.Item(0)
    3.                 If pic2.Location.Y = picFood.Location.Y Then
    4.                     'move food to new location
    5.                     RandomFoodLocation()
    6.                     'add length to snake
    7.                     snake.Add(New PictureBox().Location = New Point(pic2.Location.X, pic2.Location.Y + (growth + 17)))
    8.                     growth += 17
    9.                 End If


    The pic2 = snake.item(0) is causing problems. The compiler says that snake.item(0) is returning a boolean value of false. But it the item(0) should be a picturebox.

    I thought I forgot to add the first ball to the arraylist, but I checked and it gets added on form_load sub procedure.

    VB Code:
    1. Private Sub frmSnakeGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         snake.Add(picBall)
    3.         RandomFoodLocation()
    4.     End Sub

    Code:
    InvalidCastException was unhandled
    
    Unable to cast object of type "System.Boolean' to type 'System.Windows.Forms.PictureBox'.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: ArrayList returns boolean instead of PictureBox object

    I would advise using a List(Of PictureBox) instead of an ArrayList, example below uses a image setup as a project resource where the original image is in the app folder to make things simple here which goes with ImageLocation

    Code:
    Public Class Form1
        Private MyPictureBoxes As New List(Of PictureBox)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim P1 As New PictureBox With _
                { _
                    .Top = 10, _
                    .Left = 0, _
                    .Image = My.Resources.wanted, _
                    .Width = 400, _
                    .Height = 300, _
                    .Name = "P1", _
                    .ImageLocation = IO.Path.Combine(Application.StartupPath, "wanted.jpg") _
                }
    
            MyPictureBoxes.Add(P1)
            Me.Controls.Add(MyPictureBoxes(0))
    
            Dim MyPictBox = MyPictureBoxes(MyPictureBoxes.IndexOf(P1))
            Console.WriteLine("{0} {1} - {2}x{3}", _
                              MyPictBox.Name, _
                              MyPictBox.ImageLocation, _
                              MyPictBox.Location.Y, _
                              MyPictBox.Location.X)
    
        End Sub
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Re: ArrayList returns boolean instead of PictureBox object

    Thanks for the replying and your suggesting on using a List instead of ArrayList. I keep forgetting about List(Of).

    Also, I was looking at my code again and seen another problem.

    Here is the corrected code:

    VB Code:
    1. snake.Add(New PictureBox())
    2.                     snake.Item(snake.Count - 1).Location = New Point(pic2.Location.X, pic2.Location.Y + (growth + 17))

    I was only adding the New PictureBox().Location = New Point..... stuff and not the actual PictureBox. I didn't see my errors until I changed from ArrayList to List(Of) and received the a conversion error. So I looked at the code and figured out that Visual Basic doesn't interpret code the way that I understand it. I wanted Visual Basic to add a a new PictureBox and set the location property with the appropriate settings.

    I also noticed I haven't set the new PictureBox that the program creates to any particular image yet.

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