When trying to run this code, i get the error: System.NullReferenceException was unhandled.
My Code (Very Simple):
Public Class form1
Dim paints As System.Drawing.Graphics
Dim afont As New System.Drawing.Font("Arial", 22, FontStyle.Regular)
Dim x As Integer = 10
Dim y As Integer = 10
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This Doesn't Work!
paints.DrawString(
"Arctic Games Programming", afont, Brushes.Black, x, y)
End Sub Private Sub form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint 'This Code Works
e.Graphics.DrawString(
"Arctic Games Programming", afont, Brushes.Black, x, y) End SubEnd
Class
Text Drawing Error! Please Help!!
Simon OConnor - MVP
paints = Me.CreateGraphics()
That's not necessary though, the Paint event handler will take care of painting.
Joeouts
Gordon Radley
Scott Coffey
Eviakhan
In your code the variable paints is never being set to a valid Graphics object, in the load function you could simply insert a line previously of:
paints = Graphics.FromHwnd(Me.Handle)
While this will not break at runtime it will also be effectively worthless as your form1_Load method is firing before the form is ever being drawn and when it is drawn your previous drawing is lost and then redrawn again from form1_Paint.
Aidin