Hi

How do you draw GDI+ graphics within a class? In my normal starting form, I use the form_paint event but the seperate class doesn't seem to accept the _paint event. So I instead put my gaphics code in the same area as my second classes form propertoes.

Class SenateTown

Dim SenateTown as new form
Dim g as graphics
Public img as new bitmap("PC.bmp")

Public Function FormProperties
SenateTown.enabled = true
SenateTown.visible = true
SenateTown.Height = 600
SenateTown.Width = 600
g.drawgraphics(img, 0, 0)
End Function

But when I try this, it comes up with an error message saying "object reference not set to an instance of an object." I think I understand what it means. It sounds as if its stating that I haven't set the graphics to draw on a particular form, as I have two, but I don't know how to tell it to draw on a particuliar form. instead of g.drawimage... I tried me.g.drawimage... but that came up with the same result.

Thankyou

it comes up with an error message saying "object reference not set to an instance of an object."

That's because variable 'g' doesn't point to anywhere.

You can make your drawing in a separate class but you'll have to provide some (control) to draw on. Here's an example how it could be done

Option Explicit On
Option Strict On

Imports System.Drawing

Class SenateTown

  ' Inherit from the Form, we need Refresh method
  Inherits Form

  Dim _SenateTown As New Form
  Public img As New Bitmap("D:\PC.bmp")

  Public Sub FormProperties()
    Dim g As Graphics
    _SenateTown.Enabled = True
    _SenateTown.Visible = True
    _SenateTown.Height = 600
    _SenateTown.Width = 600
    ' CreateGraphics creates something we can actually draw on to (in this control)
    g = _SenateTown.CreateGraphics
    g.DrawImageUnscaled(img, 0, 0)
  End Sub

End Class

and you create a new instance

Dim MyTown As SenateTown

MyTown = New SenateTown
MyTown.FormProperties()
' Call refresh to force repainting of the client area
MyTown.Refresh()

Hi

Thanks! It works perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.