Error trying to draw on form
-
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.CreateGraphicsPrivate 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
-
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.CreateGraphicsPrivate 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
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
-
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.CreateGraphicsPrivate 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
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.GraphicsFor 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