Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Error trying to draw on form

Error trying to draw on form

Scheduled Pinned Locked Moved Visual Basic
graphicsphpcomgame-devhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    o m n i
    wrote on last edited by
    #1

    Ok I'm trying to make a simple inkball type game. But, I'm getting some wierd errors. Here is my code.

    Public Class Form1
    Public ink() As Point
    Public g As Graphics = Me.CreateGraphics

    Private Sub Panel1\_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim p = ink.Length
            ink(p).X = e.X
            ink(p).Y = e.Y
        End If
    End Sub
    
    Private Sub Panel1\_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        For Each p As Point In ink
            g.DrawRectangle(Pens.Black, New Rectangle(p.X, p.Y, 1, 1))
        Next
    End Sub
    

    End Class

    I've tried changing g to be Panel1.creategraphics, since the panel is what it would be drawing on but that causes even more problems. The errors I am getting are all 'Object Reference Not set to an instance of an object."

    Have you tried the Krypton Toolkit? http://www.componentfactory.com/free-windows-forms-controls.php

    B D 2 Replies Last reply
    0
    • O o m n i

      Ok I'm trying to make a simple inkball type game. But, I'm getting some wierd errors. Here is my code.

      Public Class Form1
      Public ink() As Point
      Public g As Graphics = Me.CreateGraphics

      Private Sub Panel1\_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
          If e.Button = Windows.Forms.MouseButtons.Left Then
              Dim p = ink.Length
              ink(p).X = e.X
              ink(p).Y = e.Y
          End If
      End Sub
      
      Private Sub Panel1\_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
          For Each p As Point In ink
              g.DrawRectangle(Pens.Black, New Rectangle(p.X, p.Y, 1, 1))
          Next
      End Sub
      

      End Class

      I've tried changing g to be Panel1.creategraphics, since the panel is what it would be drawing on but that causes even more problems. The errors I am getting are all 'Object Reference Not set to an instance of an object."

      Have you tried the Krypton Toolkit? http://www.componentfactory.com/free-windows-forms-controls.php

      B Offline
      B Offline
      Bharat Jain
      wrote on last edited by
      #2

      I really don't understand what you are trying to do , but the error you are getting is because you have not initialized the array ink() , it is still Nothing and hence are getting the error , was not able to understand the logic you are trying to apply , looking at the code it don't think it is going draw anything :( . The only thing i can suggest is initialized the array before using it.

      -Regards Bharat Jain bharat.jain.nagpur@gmail.com

      1 Reply Last reply
      0
      • O o m n i

        Ok I'm trying to make a simple inkball type game. But, I'm getting some wierd errors. Here is my code.

        Public Class Form1
        Public ink() As Point
        Public g As Graphics = Me.CreateGraphics

        Private Sub Panel1\_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim p = ink.Length
                ink(p).X = e.X
                ink(p).Y = e.Y
            End If
        End Sub
        
        Private Sub Panel1\_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            For Each p As Point In ink
                g.DrawRectangle(Pens.Black, New Rectangle(p.X, p.Y, 1, 1))
            Next
        End Sub
        

        End Class

        I've tried changing g to be Panel1.creategraphics, since the panel is what it would be drawing on but that causes even more problems. The errors I am getting are all 'Object Reference Not set to an instance of an object."

        Have you tried the Krypton Toolkit? http://www.componentfactory.com/free-windows-forms-controls.php

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        The graphics object you created is one for the form, not the panel. You get the graphics object for the panel from the Panel1 Paint event, like this:

        Private Sub Panel1_Paint(ByVal sender as System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim g As Graphics = e.Graphics

        For Each p As Point In Ink
            g.Draw....
        Next
        

        End Sub

        But, of course, you'll have to modify the paint code to check to see if there are any points at all in the Ink array.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups