none
Im having trouble making picboxs in vb.net RRS feed

  • Question

  • I've made a simple game to try and get better at coding. The point of the game is having a character dodge falling blocks. What i'm trying to do is make picture boxes that hold the blocks picture that will fall. I did a project like this in school but not making them fall all we did was generate them onto random spots on screen. The problem i'm having is getting them to load on screen. Ill share the part of my code that doesn't seem to work and if needed share the whole thing. thank you to whoever takes their time to help me out. 

    Public Sub addBlock1()
            Dim p As PictureBox
            For x = 4 To BLOCKS

                p = New PictureBox

                p.Height = 62
                p.Width = 48

                'set properties of the new block
                p.SizeMode = PictureBoxSizeMode.StretchImage
                p.Image = My.Resources.block3
                p.Visible = True
                p.Left = generator.Next(0, Height)
                p.Top = picBlock1.Top

                'for the form to recognize this new PictureBox, 
                'you need to add it to the forms controls list 
                Me.Controls.Add(p)
                picsList.Add(p)

            Next
        End Sub

        Private Sub tmrAddBlocks_Tick(sender As Object, e As EventArgs) Handles tmrAddBlocks.Tick
            tmrAddBlocks.Interval = 1500
            Call addBlock1()

            For Each p In picsList
                If p.Top >= 560 Then
                    Score = Score + 10
                End If
            Next
        End Sub

    A few notes..

    the timer is enabled, i have it set to enabled when i press start. 

    The code returns this error

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    at this line

    p.Left = generator.Next(0, Height)

    Tuesday, June 12, 2018 12:04 AM

All replies

  • The code returns this error

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    at this line

    p.Left = generator.Next(0, Height)

    The message is telling you that the variable 'generator' refers to Nothing - it has not been initialised so that it refers to an object.

    You should look at how that variable is declared and used, and work out what sort of object you need to initialise it to.  What object has a Next method?  Should generator be initialised to a Random object? If so, that usually happens in a form load method.

    Tuesday, June 12, 2018 12:20 AM
  • Hmm.

    You should start over now and learn to just draw rectangles in the paint event for the form or picturebox controls etc.

    Instead of a bunch of picturebox controls you make a list of rectangle graphic objects.

    Or better yet you make a class for your play object with all the info it needs like size, location, color, health points, weapons and on and on...

    If you want we can help you set that up if you describe what you had in mind. What is your current programming skill level etc.



    Tuesday, June 12, 2018 12:23 AM
  • That would be nice. I figured out why it was returning the error, i had generator dim as random when it should have been dim as new random. the same problem i am having now is its not generating the pictures in the picture box and if it is how would i find out the coordinates of where to make them. I have very basic knowledge in vb, entry level, i just took a half year course in high school and it was interesting and fun so i want to pursue it. If having me send me whole code would help you know what i'm trying to do better, although i will briefly describe it now. The game starts off loaded into one form that says the name of the game, a "new game" button and an "exit" button. Clicking on new game prompts you to form 2 while hiding form 1. form 2 loads by saying chose a character, i have 3 choices of characters. When you chose one the others hide and the game doesn't start yet, not until you click the start button which then goes away when you click it and you are now able to move the character. The next step would be the blocks falling from the sky but this is the problem i am having trouble with. If there is any info you need let me know and thank you for helping me!
    Tuesday, June 12, 2018 12:43 AM
  • That would be nice. I figured out why it was returning the error, i had generator dim as random when it should have been dim as new random. the same problem i am having now is its not generating the pictures in the picture box and if it is how would i find out the coordinates of where to make them. I have very basic knowledge in vb, entry level, i just took a half year course in high school and it was interesting and fun so i want to pursue it. If having me send me whole code would help you know what i'm trying to do better, although i will briefly describe it now. The game starts off loaded into one form that says the name of the game, a "new game" button and an "exit" button. Clicking on new game prompts you to form 2 while hiding form 1. form 2 loads by saying chose a character, i have 3 choices of characters. When you chose one the others hide and the game doesn't start yet, not until you click the start button which then goes away when you click it and you are now able to move the character. The next step would be the blocks falling from the sky but this is the problem i am having trouble with. If there is any info you need let me know and thank you for helping me!

    ferra,

    Ok. Well I guess its is best to finish this one then as it still has most of the basics all good to learn and then it answers some of your questions. Pic box or rectangle basically the same.

    Acamar has pointed out things and you said:

    "its not generating the pictures in the picture box and if it is how would i find out the coordinates of where to make them."

    To start you should put

         Dim p As NEW PictureBox

    inside the loop and note the new.

    That way each loop you make a new picbox and add a new reference object to the list of all the pic boxes. Thus there is a unique memory reference to each picturebox control in the list.

    The way you have it now you make one p picturebox before the loop and then update it each time through the loop but its still only one picturebox. So you painted picturebox1 - 27 times instead of making 27 different pictureboxes each with its own picture.

    PS Just get a few blocks moving on the screen to start with. Then add to that basic game loop as you get it debugged and learn.

    Part of learning to program is learning to debug the program so you need to learn that as you go.


    Tuesday, June 12, 2018 1:22 AM
  • Hi mferraro23,

    According to your error message, I guess that you could not new one Random in your code, you can do this like this:

    Dim generator As New Random

    And do you want to do like this? 

    Public Class Form1
        Dim p As PictureBox = New PictureBox
        Dim score As Integer
        Public Sub addBlock1()
            p.Height = 62
            p.Width = 48
            p.SizeMode = PictureBoxSizeMode.StretchImage
            p.Image = ImageList1.Images(0)
            p.Visible = True
            Dim r As Random = New Random()
            p.Left = r.Next(0, Height)
            p.Top += 200
            Me.Controls.Add(p)
        End Sub
        Private Sub tmrAddBlocks_Tick(sender As Object, e As EventArgs) Handles tmrAddBlocks.Tick
            tmrAddBlocks.Interval = 1500
            Call addBlock1()
            If p.Top >= 480 Then
                p.Top = 0
                score = score + 10
            End If
            Label1.Text = score
        End Sub
    End Class
    
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Monday, June 18, 2018 7:00 AM
    Tuesday, June 12, 2018 5:51 AM
    Moderator
  • The next step would be the blocks falling from the sky but this is the problem i am having trouble with. If there is any info you need let me know and thank you for helping me!

    Hi

    Here is one example of blocks (Images / Icons) falling from the sky - with a wobble! Not a PictureBox to be found anywhere :)

    ' Form1 with Timer1, copy/replace
    ' default code With this code
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim SkyBlocks As New List(Of SkyBlock)
      Dim r As New Random
      ' folder with a few images / Icons
      Dim ImPath As String = "C:\Users\lesha\Documents\VB Resources\Icons"
      Dim ImPaths As List(Of String) = IO.Directory.GetFiles(ImPath).ToList
      Dim Im As New List(Of Image)
      Dim bottMargin As Integer = 100
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = New Size(600, 400)
        DoubleBuffered = True
        ' fetch Images/Icons to List
        For Each s As String In ImPaths
          Im.Add(Image.FromFile(s))
        Next
        ' create new SkyBlocks
        For i As Integer = 1 To 8
          Dim sb As New SkyBlock
          With sb
            .width = 50
            .Height = 50
            .y = -r.Next(50, 80)
            .x = i * 60 + r.Next(20, 40)
            .Speed = r.Next(1, 4)
            .Image = Im(r.Next(0, Im.Count))
          End With
          SkyBlocks.Add(sb)
        Next
        Timer1.Interval = 50
        Timer1.Enabled = True
      End Sub
      Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        For Each sb As SkyBlock In SkyBlocks
          With sb
            e.Graphics.DrawImage(.Image, .x, .y, .width, .Height)
          End With
        Next
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        For Each sb As SkyBlock In SkyBlocks
          With sb
            .y += .Speed
            If .y > ClientRectangle.Height - bottMargin Then
              .y = -r.Next(50, 80)
              .Speed = r.Next(1, 4)
              .Image = Im(r.Next(0, Im.Count - 1))
            End If
            .x += r.Next(-2, 3)
          End With
        Next
        Invalidate()
      End Sub
      Class SkyBlock
        Property x As Integer
        Property y As Integer
        Property width As Integer
        Property Height As Integer
        Property Image As Image
        Property Speed As Integer
      End Class
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, June 14, 2018 8:36 PM
    Tuesday, June 12, 2018 5:58 AM
  • hey thanks for the help. Now when i run this it makes picture boxes but they wont move or fall down. How would i go about getting them to fall? Would i make a new timer? The problem i seem to have is how would i access each p after its made, i would have to add them to a list correct?
    Thursday, June 14, 2018 7:02 PM
  • hey thanks for the help. Now when i run this it makes picture boxes but they wont move or fall down. How would i go about getting them to fall? Would i make a new timer? The problem i seem to have is how would i access each p after its made, i would have to add them to a list correct?


    Thats right. Similar to what Leshay's example shows with a screen object class. But, instead of the list of SkyBlock you would have a list of picturebox (you could also loop through the form controls but its good to make your own list).

    Also notice Leshay's example makes the screen object list in the form load event, then increments the locations in the timer, and finally draws the list in the paint event.

    Since you use pictureboxes you dont have to draw the screen objects of course.

    See how Leshay loops through the list of screen object skyblock in the timer and increments the  position. So for a picture box it would be similar:

    PS Plus see how Cherry's example adds more  blocks to the list of form controls in the timer event. You might want to do that in addition or instead of the form load event.

    'loop through the list
    
        For Each p As Picturebox In MyPicsList
    
              'move the picturebox down
    
              p.Top += 10
    
        Next




    Thursday, June 14, 2018 7:34 PM