473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Error "Object reference not set to an instance of an object"

When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black,
0, 0)

Why is this?

Marc
Nov 19 '05 #1
18 28697
Dim g as New System.Drawing.Graphics

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black,
0, 0)

Why is this?

Marc

Nov 19 '05 #2
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing.Grapichics.DrawString

or import System.Drawing.Graphics and just call DrawString.
"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black,
0, 0)

Why is this?

Marc

Nov 19 '05 #3
Another error I'm afraid (with both solutions):

"Reference to a non-shared member requires an object reference."

Marc

"CJ Taylor" <ct*****@mortonwelding.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing.Grapichics.DrawString

or import System.Drawing.Graphics and just call DrawString.
"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0)

Why is this?

Marc


Nov 19 '05 #4
Ahhh ok...

You have to get the drawing object from somewhere in your application. it
usually comes from a repaint or such. I forgot about little things like
this (its still early).

Like, a repaint where PaintEventArgs is passed.
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:ub*************@TK2MSFTNGP10.phx.gbl...
Another error I'm afraid (with both solutions):

"Reference to a non-shared member requires an object reference."

Marc

"CJ Taylor" <ct*****@mortonwelding.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
Um..

Try to not declare it as an instance... sorry I didn't point that out
earlier...

Just directly call System.Drawing.Grapichics.DrawString

or import System.Drawing.Graphics and just call DrawString.
"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0)

Why is this?

Marc



Nov 19 '05 #5
Hello,

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphics()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromImage(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #6
Hello,

"CJ Taylor" <ct*****@mortonwelding.com> schrieb:
Dim g as New System.Drawing.Graphics


Did you try this?

;-)))

Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 19 '05 #7
Hmm... I forgot about that too. . Thanks Herfriend! Always a help from
pulling us out of digging a hole. =)
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphics()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromImage(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Nov 19 '05 #8
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)

Sub Print()
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
PrintDocument1.Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphics()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromImage(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Nov 19 '05 #9
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The PrintPageEventArgs
object has a Graphics property. You need to draw on this Graphics object to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:
http://msdn.microsoft.com/library/de...et12242002.asp
http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)

Sub Print()
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
PrintDocument1.Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
When I try this in my code I alwas get an errormessage: "Object

reference
not set to an instance of an object"

Dim g As System.Drawing.Graphics
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),

Brushes.Black,
0, 0)


You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphics()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromImage(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


Nov 19 '05 #10
At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer, y
As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The PrintPageEventArgs object has a Graphics property. You need to draw on this Graphics object to have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:
http://msdn.microsoft.com/library/de...et12242002.asp http://msdn.microsoft.com/library/de...et01282003.asp
Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)

Sub Print()
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
PrintDocument1.Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hello,

"Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> When I try this in my code I alwas get an errormessage: "Object
reference
> not set to an instance of an object"
>
> Dim g As System.Drawing.Graphics
> g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black,
> 0, 0)

You never assign an instance of the Graphics class to the instance
variable g. Use something like this:

\\\
Dim g As Graphics = Me.CreateGraphics()
///

or

\\\
Dim bmp As Bitmap = ...
Dim g As Graphics = Graphics.FromImage(bmp)
///

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet



Nov 19 '05 #11
Are you talking about adding another handler that happens when printpage is
fireD? I'm confused.

"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer, y As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The

PrintPageEventArgs
object has a Graphics property. You need to draw on this Graphics object

to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:

http://msdn.microsoft.com/library/de...et12242002.asp

http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)

Sub Print()
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
PrintDocument1.Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht news:ue**************@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> "Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> > When I try this in my code I alwas get an errormessage: "Object
> reference
> > not set to an instance of an object"
> >
> > Dim g As System.Drawing.Graphics
> > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> Brushes.Black,
> > 0, 0)
>
> You never assign an instance of the Graphics class to the instance
> variable g. Use something like this:
>
> \\\
> Dim g As Graphics = Me.CreateGraphics()
> ///
>
> or
>
> \\\
> Dim bmp As Bitmap = ...
> Dim g As Graphics = Graphics.FromImage(bmp)
> ///
>
> HTH,
> Herfried K. Wagner
> --
> MVP · VB Classic, VB .NET
> http://www.mvps.org/dotnet
>
>



Nov 19 '05 #12
No, (Or maybe yes, I don't know) In the first lines of code I showed, all
the work is done in the Eventhandler:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("Tekst", New Font("Arial", 12,
FontStyle.Bold),Brushes.Black, 0, 0)
End Sub

But what if I want to build the document outside the EventHandler and than
pass it to the Eventhandler to do the printing. Something like this:

Somewhere in a Procedure:
PrintDocument_PrintPage("Tekst", 0, 10)

Eventhandler:
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, cTekst As String, x As Integer,
y As Integer
e.Graphics.DrawString(cTekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, the code I show doesn't work, but is there another way of doing
this?

Marc

"CJ Taylor" <ct*****@mortonwelding.com> schreef in bericht
news:eq**************@tk2msftngp13.phx.gbl...
Are you talking about adding another handler that happens when printpage is fireD? I'm confused.

"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer, y
As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The

PrintPageEventArgs
object has a Graphics property. You need to draw on this Graphics object to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:

http://msdn.microsoft.com/library/de...et12242002.asp

http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
> I'm a little bit further:
>
> Module Procedures
> Friend WithEvents PrintDocument1 As New
> System.Drawing.Printing.PrintDocument()
> Dim bmp As New Bitmap(1, 1)
> Dim g As Graphics = Graphics.FromImage(bmp)
>
> Sub Print()
> g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> Brushes.Black, 0, 0)
> PrintDocument1.Print()
> End Sub
> End Module
>
> This prints a blank page, not the word "test"???
>
> Marc
>
> "Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht > news:ue**************@TK2MSFTNGP10.phx.gbl...
> > Hello,
> >
> > "Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> > > When I try this in my code I alwas get an errormessage: "Object
> > reference
> > > not set to an instance of an object"
> > >
> > > Dim g As System.Drawing.Graphics
> > > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> > Brushes.Black,
> > > 0, 0)
> >
> > You never assign an instance of the Graphics class to the instance
> > variable g. Use something like this:
> >
> > \\\
> > Dim g As Graphics = Me.CreateGraphics()
> > ///
> >
> > or
> >
> > \\\
> > Dim bmp As Bitmap = ...
> > Dim g As Graphics = Graphics.FromImage(bmp)
> > ///
> >
> > HTH,
> > Herfried K. Wagner
> > --
> > MVP · VB Classic, VB .NET
> > http://www.mvps.org/dotnet
> >
> >
>
>



Nov 19 '05 #13
If I undertand you right then use RaiseEvent or AddHandler.
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
No, (Or maybe yes, I don't know) In the first lines of code I showed, all
the work is done in the Eventhandler:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.DrawString("Tekst", New Font("Arial", 12,
FontStyle.Bold),Brushes.Black, 0, 0)
End Sub

But what if I want to build the document outside the EventHandler and than
pass it to the Eventhandler to do the printing. Something like this:

Somewhere in a Procedure:
PrintDocument_PrintPage("Tekst", 0, 10)

Eventhandler:
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, cTekst As String, x As Integer, y As Integer
e.Graphics.DrawString(cTekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, the code I show doesn't work, but is there another way of doing
this?

Marc

"CJ Taylor" <ct*****@mortonwelding.com> schreef in bericht
news:eq**************@tk2msftngp13.phx.gbl...
Are you talking about adding another handler that happens when printpage

is
fireD? I'm confused.

"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles

PrintDocument1.PrintPage
e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer,
y
As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
> Marc,
> You are drawing on a bitmap, not on the document!
>
> To draw on the document you need to handle the PrintDocument.PrintPage > event. This event has a PrintPageEventArgs parameter. The
PrintPageEventArgs
> object has a Graphics property. You need to draw on this Graphics

object to
> have it shown on the document you are attempting to print.
>
> For a reasonable complete sample of using PrintDocument see:
>

http://msdn.microsoft.com/library/de...et12242002.asp
>

http://msdn.microsoft.com/library/de...et01282003.asp
>
> Hope this helps
> Jay
>
>
> "Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
> news:uz**************@tk2msftngp13.phx.gbl...
> > I'm a little bit further:
> >
> > Module Procedures
> > Friend WithEvents PrintDocument1 As New
> > System.Drawing.Printing.PrintDocument()
> > Dim bmp As New Bitmap(1, 1)
> > Dim g As Graphics = Graphics.FromImage(bmp)
> >
> > Sub Print()
> > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), > > Brushes.Black, 0, 0)
> > PrintDocument1.Print()
> > End Sub
> > End Module
> >
> > This prints a blank page, not the word "test"???
> >
> > Marc
> >
> > "Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in

bericht
> > news:ue**************@TK2MSFTNGP10.phx.gbl...
> > > Hello,
> > >
> > > "Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> > > > When I try this in my code I alwas get an errormessage: "Object > > > reference
> > > > not set to an instance of an object"
> > > >
> > > > Dim g As System.Drawing.Graphics
> > > > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> > > Brushes.Black,
> > > > 0, 0)
> > >
> > > You never assign an instance of the Graphics class to the instance > > > variable g. Use something like this:
> > >
> > > \\\
> > > Dim g As Graphics = Me.CreateGraphics()
> > > ///
> > >
> > > or
> > >
> > > \\\
> > > Dim bmp As Bitmap = ...
> > > Dim g As Graphics = Graphics.FromImage(bmp)
> > > ///
> > >
> > > HTH,
> > > Herfried K. Wagner
> > > --
> > > MVP · VB Classic, VB .NET
> > > http://www.mvps.org/dotnet
> > >
> > >
> >
> >
>
>



Nov 19 '05 #14
Marc,
How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like: OOP 101? :-|

You cannot modfy the signature of the event handler in .NET. Generally you
need to set class level variables (preferable private) with this data then
the Print event can act on it. Remember the Print event prints an entire
page. It will be called once for each page. Via the PrintPageEventArgs it
lets PrintDocument know there are more pages to print.

Multiple objects can handle the PrintPage event for a single PrintDocument
object. Each could print their 'own part' of the page. This unfortunately
would get very confusing quickly!

Generally what I do is have a collection of objects that I want to print as
a class level variable. This collection of objects all derive from the same
class or implement the same interface. The base class has a Draw method in
it. My PrintPage event handler then uses a For Each on the collection
calling Draw for each object.

Something like:

Public MustInherit Class PrintableElement

Public MustOverride Sub Draw(ByVal gr As Graphics)

End Class

Public Class PrintableString

Private ReadOnly m_tekst As String
Private ReadOnly m_x As Integer
Private ReadOnly m_y As Integer

Public Sub New(tekst as string, x as integer, y as integer)
m_tekst = tekst
m_x = x
m_y = y
End Sub

Public Overrides Sub Draw(ByVal gr As Graphics)
gr.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold), _
Brushes.Black, 0, 0)
End Sub

End Sub

' m_elements contains classes that derive
' from PrintableElements
Private m_elements As ArrayList
m_elements.Add(New PrintableString("Tekst", 0, 0))

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

For Each element As PrintableElement In m_elements
element.Draw(e.Graphics)
Next
End Sub
Of course if there are multiple pages, then there is more work involved.

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl... At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer, y As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The

PrintPageEventArgs
object has a Graphics property. You need to draw on this Graphics object

to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:

http://msdn.microsoft.com/library/de...et12242002.asp

http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
I'm a little bit further:

Module Procedures
Friend WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Dim bmp As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(bmp)

Sub Print()
g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
PrintDocument1.Print()
End Sub
End Module

This prints a blank page, not the word "test"???

Marc

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht news:ue**************@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> "Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> > When I try this in my code I alwas get an errormessage: "Object
> reference
> > not set to an instance of an object"
> >
> > Dim g As System.Drawing.Graphics
> > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> Brushes.Black,
> > 0, 0)
>
> You never assign an instance of the Graphics class to the instance
> variable g. Use something like this:
>
> \\\
> Dim g As Graphics = Me.CreateGraphics()
> ///
>
> or
>
> \\\
> Dim bmp As Bitmap = ...
> Dim g As Graphics = Graphics.FromImage(bmp)
> ///
>
> HTH,
> Herfried K. Wagner
> --
> MVP · VB Classic, VB .NET
> http://www.mvps.org/dotnet
>
>



Nov 19 '05 #15
I think I get it. I'll try this out to be sure ;-)

Thanks a lot for all the answers!

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:ux**************@tk2msftngp13.phx.gbl...
Marc,
How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like: OOP 101? :-|

You cannot modfy the signature of the event handler in .NET. Generally you
need to set class level variables (preferable private) with this data then
the Print event can act on it. Remember the Print event prints an entire
page. It will be called once for each page. Via the PrintPageEventArgs it
lets PrintDocument know there are more pages to print.

Multiple objects can handle the PrintPage event for a single PrintDocument
object. Each could print their 'own part' of the page. This unfortunately
would get very confusing quickly!

Generally what I do is have a collection of objects that I want to print

as a class level variable. This collection of objects all derive from the same class or implement the same interface. The base class has a Draw method in
it. My PrintPage event handler then uses a For Each on the collection
calling Draw for each object.

Something like:

Public MustInherit Class PrintableElement

Public MustOverride Sub Draw(ByVal gr As Graphics)

End Class

Public Class PrintableString

Private ReadOnly m_tekst As String
Private ReadOnly m_x As Integer
Private ReadOnly m_y As Integer

Public Sub New(tekst as string, x as integer, y as integer)
m_tekst = tekst
m_x = x
m_y = y
End Sub

Public Overrides Sub Draw(ByVal gr As Graphics)
gr.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold), _ Brushes.Black, 0, 0)
End Sub

End Sub

' m_elements contains classes that derive
' from PrintableElements
Private m_elements As ArrayList
m_elements.Add(New PrintableString("Tekst", 0, 0))

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

For Each element As PrintableElement In m_elements
element.Draw(e.Graphics)
Next
End Sub


Of course if there are multiple pages, then there is more work involved.

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uQ**************@TK2MSFTNGP10.phx.gbl...
At last, it prints.

But (of course :-) ) I have another question:

I used this code:

Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles

PrintDocument1.PrintPage
e.Graphics.DrawString("Tekst", New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, 0, 0)
End Sub

How can I send printdata from outside the PrintPage eventhandler to the
eventhandler. Something like:

Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs, Tekst As String, x As Integer, y
As Integer) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, x, y)
End Sub

Of course, this desn't work...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:%2***************@tk2msftngp13.phx.gbl...
Marc,
You are drawing on a bitmap, not on the document!

To draw on the document you need to handle the PrintDocument.PrintPage
event. This event has a PrintPageEventArgs parameter. The

PrintPageEventArgs
object has a Graphics property. You need to draw on this Graphics object to
have it shown on the document you are attempting to print.

For a reasonable complete sample of using PrintDocument see:

http://msdn.microsoft.com/library/de...et12242002.asp

http://msdn.microsoft.com/library/de...et01282003.asp

Hope this helps
Jay
"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uz**************@tk2msftngp13.phx.gbl...
> I'm a little bit further:
>
> Module Procedures
> Friend WithEvents PrintDocument1 As New
> System.Drawing.Printing.PrintDocument()
> Dim bmp As New Bitmap(1, 1)
> Dim g As Graphics = Graphics.FromImage(bmp)
>
> Sub Print()
> g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> Brushes.Black, 0, 0)
> PrintDocument1.Print()
> End Sub
> End Module
>
> This prints a blank page, not the word "test"???
>
> Marc
>
> "Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schreef in bericht > news:ue**************@TK2MSFTNGP10.phx.gbl...
> > Hello,
> >
> > "Microsoft" <Marc.VanSchandevijl@_nospam_.c-luma.com> schrieb:
> > > When I try this in my code I alwas get an errormessage: "Object
> > reference
> > > not set to an instance of an object"
> > >
> > > Dim g As System.Drawing.Graphics
> > > g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold),
> > Brushes.Black,
> > > 0, 0)
> >
> > You never assign an instance of the Graphics class to the instance
> > variable g. Use something like this:
> >
> > \\\
> > Dim g As Graphics = Me.CreateGraphics()
> > ///
> >
> > or
> >
> > \\\
> > Dim bmp As Bitmap = ...
> > Dim g As Graphics = Graphics.FromImage(bmp)
> > ///
> >
> > HTH,
> > Herfried K. Wagner
> > --
> > MVP · VB Classic, VB .NET
> > http://www.mvps.org/dotnet
> >
> >
>
>



Nov 19 '05 #16
Yes, I can follow this (I use Visual Foxpro for a couple of years, so I know
about Abstract Classes).

The problem is that it doesn't work this way.

Here's what I have so far:

Public MustInherit Class PrintableElement
Public MustOverride Sub Draw(ByVal gr As Graphics)
End Class

Public Class PrintableString
Inherits PrintableElement
Private ReadOnly m_tekst As String
Private ReadOnly m_x As Integer
Private ReadOnly m_y As Integer

Public Sub New(ByVal tekst As String, ByVal x As Integer, ByVal y As
Integer)
m_tekst = tekst
m_x = x
m_y = y
End Sub

Public Overrides Sub Draw(ByVal gr As System.Drawing.Graphics)
gr.DrawString(m_tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, m_x, m_y)
End Sub
End Class

Module Module1
Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Public m_elements As ArrayList

Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim element As PrintableElement
For Each element In m_elements
element.Draw(e.Graphics)
Next
End Sub
End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
m_elements.Add(New PrintableString("Tekst", 0, 10))
End Sub

When I click on the button I get an error:
'Object reference not set to an instance of an error'

That's what it all began with...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:u5**************@tk2msftngp13.phx.gbl...
Marc,
Neither of these belong in the PrintPage event handler, you need to fill the ArrayList before you call PrintDocument.Print method!
Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim element As New PrintableElement()


You don't want an instance of a class here, you just want a variable:
Sub PrintDocument1_PrintPage( ...


Dim element As PrintableElement
For Each element In m_elements
element.Draw(e.Graphics)
Next
End Sub


You use 'As New' when you want an instance of a class created, here you
don't as the For Each will set the element variable to each instance of

the class in the ArrayList.

PrintableElement is what is known as a Abstract Base Class . It is a
template used to define other objects, Abstract Base Classes are used for
Polymorphism. Your PrintPage event handler knows about the PrintableElement class, it uses the Draw method. The Draw method is polymorphic. However the array list actually contains PrintableString elements. The PrintableString
is what is known as a Concrete class. It offers an implementation for the
PrintableElement.Draw method. Although the PrintPage method is calling
PrintableElement.Draw indirectly it is executing the PrintableString.Draw
method. You can extend this to have a PrintableLine class that inherits from PrintableElement. The PrintableLine.Draw method would draw a line, instead
of drawing text. The PrintPage method doesn't care as it is still calling
PrintableElement.Draw, for a PrintableLine object in the ArrayList, the
PrintableLine.Draw method would be indirectly called.

A Sample PrintableLine class:

Public Class PrintableLine
Inherits PrintableElement

Private ReadOnly m_pt1 As Point
Private ReadOnly m_pt2 As Point

Public Sub New(ByVal pt1 As Point, pt2 As Point)
m_pt1 = pt1
m_pt2 = pt2
End Sub

Public Overrides Sub Draw(ByVal gr As Graphics)
gr.DrawLine(Pens.Black, m_pt1, m_pt2)
End Sub

End Class

Hope this helps
Jay

Nov 20 '05 #17
Sorry, forgot something:

The error happens on the line

m_elements.Add(New PrintableString("Tekst", 0, 10))

Marc
Nov 20 '05 #18
Marc,
Yes, I can follow this (I use Visual Foxpro for a couple of years, so I know about Abstract Classes). I know you can follow it, I just wanted to make sure you knew.

In your Module, you define a variable for m_elements, but you do not
initialize it:
Public m_elements As ArrayList
Public m_elements As New ArrayList
When I click on the button I get an error:
'Object reference not set to an instance of an error' Normally when I get the above error, I break into the debugger and look at
the various objects associated with that line.

Having used classes in Visual Foxpro, I'm sure you understand that its safer
not to be using a Module, as the first time you print something you will be
fine, the second time. Now why am I getting double printing? I use Modules
very infrequently.

Hope this helps
Jay

"Marc" <Marc.VanSchandevijl@_nospam_.c-luma.com> wrote in message
news:uq****************@TK2MSFTNGP12.phx.gbl... Yes, I can follow this (I use Visual Foxpro for a couple of years, so I know about Abstract Classes).

The problem is that it doesn't work this way.

Here's what I have so far:

Public MustInherit Class PrintableElement
Public MustOverride Sub Draw(ByVal gr As Graphics)
End Class

Public Class PrintableString
Inherits PrintableElement
Private ReadOnly m_tekst As String
Private ReadOnly m_x As Integer
Private ReadOnly m_y As Integer

Public Sub New(ByVal tekst As String, ByVal x As Integer, ByVal y As
Integer)
m_tekst = tekst
m_x = x
m_y = y
End Sub

Public Overrides Sub Draw(ByVal gr As System.Drawing.Graphics)
gr.DrawString(m_tekst, New Font("Arial", 12, FontStyle.Bold),
Brushes.Black, m_x, m_y)
End Sub
End Class

Module Module1
Private WithEvents PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Public m_elements As ArrayList

Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim element As PrintableElement
For Each element In m_elements
element.Draw(e.Graphics)
Next
End Sub
End Module

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
m_elements.Add(New PrintableString("Tekst", 0, 10))
End Sub

When I click on the button I get an error:
'Object reference not set to an instance of an error'

That's what it all began with...

Marc

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> schreef in
bericht news:u5**************@tk2msftngp13.phx.gbl...
Marc,
Neither of these belong in the PrintPage event handler, you need to fill

the
ArrayList before you call PrintDocument.Print method!
Sub PrintDocument1_PrintPage(ByVal Sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles

PrintDocument1.PrintPage
Dim element As New PrintableElement()


You don't want an instance of a class here, you just want a variable:
Sub PrintDocument1_PrintPage( ...


Dim element As PrintableElement
For Each element In m_elements
element.Draw(e.Graphics)
Next
End Sub


You use 'As New' when you want an instance of a class created, here you
don't as the For Each will set the element variable to each instance of

the
class in the ArrayList.

PrintableElement is what is known as a Abstract Base Class . It is a
template used to define other objects, Abstract Base Classes are used for Polymorphism. Your PrintPage event handler knows about the

PrintableElement
class, it uses the Draw method. The Draw method is polymorphic. However

the
array list actually contains PrintableString elements. The PrintableString is what is known as a Concrete class. It offers an implementation for the PrintableElement.Draw method. Although the PrintPage method is calling
PrintableElement.Draw indirectly it is executing the PrintableString.Draw method. You can extend this to have a PrintableLine class that inherits

from
PrintableElement. The PrintableLine.Draw method would draw a line, instead of drawing text. The PrintPage method doesn't care as it is still calling PrintableElement.Draw, for a PrintableLine object in the ArrayList, the
PrintableLine.Draw method would be indirectly called.

A Sample PrintableLine class:

Public Class PrintableLine
Inherits PrintableElement

Private ReadOnly m_pt1 As Point
Private ReadOnly m_pt2 As Point

Public Sub New(ByVal pt1 As Point, pt2 As Point)
m_pt1 = pt1
m_pt2 = pt2
End Sub

Public Overrides Sub Draw(ByVal gr As Graphics)
gr.DrawLine(Pens.Black, m_pt1, m_pt2)
End Sub

End Class

Hope this helps
Jay


Nov 20 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
0
by: Martin Widmer | last post by:
Hello again! I have a datagridview control on my form and am using VS.Net 2005. One column is set up as combo box column, and when I try to set the datasource for that combobox column at design...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
2
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM...
3
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM...
1
by: nguyentrongkha | last post by:
I have two asp.net applications that host on server 2003. I create a hyper link on one application that links to default page of the other application.Whenever users click on that link, I get a...
11
by: narpet | last post by:
Hello all, I'm hoping somebody can help me with this. I have an app that I've been developing in MS Visual Studio 2005 Pro, C#. I have connectivity to a SQL server database. All of the sudden,...
3
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose...
5
by: dotnetnovice | last post by:
Hi everybody actually i was trying to insert some records in to SQL server through C# Wndows Aplication and during that i face an error "Object reference not set to an instance of an object." and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.