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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Microsoft

    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("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
    0, 0)

    Why is this?

    Marc


  • CJ Taylor

    #2
    Re: Error "Object reference not set to an instance of an object"

    Dim g as New System.Drawing. Graphics

    "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
    news:et$0oKwaDH A.1384@TK2MSFTN GP10.phx.gbl...[color=blue]
    > 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("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
    > 0, 0)
    >
    > Why is this?
    >
    > Marc
    >
    >[/color]


    Comment

    • CJ Taylor

      #3
      Re: Error &quot;Object reference not set to an instance of an object&quot;

      Um..

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

      Just directly call System.Drawing. Grapichics.Draw String

      or import System.Drawing. Graphics and just call DrawString.


      "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
      news:et$0oKwaDH A.1384@TK2MSFTN GP10.phx.gbl...[color=blue]
      > 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("T est", New Font("Arial", 12, FontStyle.Bold) , Brushes.Black,
      > 0, 0)
      >
      > Why is this?
      >
      > Marc
      >
      >[/color]


      Comment

      • Marc

        #4
        Re: Error &quot;Object reference not set to an instance of an object&quot;

        Another error I'm afraid (with both solutions):

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

        Marc

        "CJ Taylor" <ctaylor@morton welding.com> schreef in bericht
        news:%2313SQfwa DHA.1940@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Um..
        >
        > Try to not declare it as an instance... sorry I didn't point that out
        > earlier...
        >
        > Just directly call System.Drawing. Grapichics.Draw String
        >
        > or import System.Drawing. Graphics and just call DrawString.
        >
        >
        > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
        > news:et$0oKwaDH A.1384@TK2MSFTN GP10.phx.gbl...[color=green]
        > > When I try this in my code I alwas get an errormessage: "Object[/color][/color]
        reference[color=blue][color=green]
        > > not set to an instance of an object"
        > >
        > > Dim g As System.Drawing. Graphics
        > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,[/color][/color]
        Brushes.Black,[color=blue][color=green]
        > > 0, 0)
        > >
        > > Why is this?
        > >
        > > Marc
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • CJ Taylor

          #5
          Re: Error &quot;Object reference not set to an instance of an object&quot;

          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.VanSchand evijl@_nospam_. c-luma.com> wrote in message
          news:ub5sIiwaDH A.388@TK2MSFTNG P10.phx.gbl...[color=blue]
          > Another error I'm afraid (with both solutions):
          >
          > "Reference to a non-shared member requires an object reference."
          >
          > Marc
          >
          > "CJ Taylor" <ctaylor@morton welding.com> schreef in bericht
          > news:%2313SQfwa DHA.1940@TK2MSF TNGP10.phx.gbl. ..[color=green]
          > > Um..
          > >
          > > Try to not declare it as an instance... sorry I didn't point that out
          > > earlier...
          > >
          > > Just directly call System.Drawing. Grapichics.Draw String
          > >
          > > or import System.Drawing. Graphics and just call DrawString.
          > >
          > >
          > > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
          > > news:et$0oKwaDH A.1384@TK2MSFTN GP10.phx.gbl...[color=darkred]
          > > > When I try this in my code I alwas get an errormessage: "Object[/color][/color]
          > reference[color=green][color=darkred]
          > > > not set to an instance of an object"
          > > >
          > > > Dim g As System.Drawing. Graphics
          > > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,[/color][/color]
          > Brushes.Black,[color=green][color=darkred]
          > > > 0, 0)
          > > >
          > > > Why is this?
          > > >
          > > > Marc
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Error &quot;Object reference not set to an instance of an object&quot;

            Hello,

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

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

            \\\
            Dim g As Graphics = Me.CreateGraphi cs()
            ///

            or

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

            HTH,
            Herfried K. Wagner
            --
            MVP · VB Classic, VB .NET
            Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Error &quot;Object reference not set to an instance of an object&quot;

              Hello,

              "CJ Taylor" <ctaylor@morton welding.com> schrieb:[color=blue]
              > Dim g as New System.Drawing. Graphics[/color]

              Did you try this?

              ;-)))

              Regards,
              Herfried K. Wagner
              --
              MVP · VB Classic, VB .NET
              Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



              Comment

              • CJ Taylor

                #8
                Re: Error &quot;Object reference not set to an instance of an object&quot;

                Hmm... I forgot about that too. . Thanks Herfriend! Always a help from
                pulling us out of digging a hole. =)


                "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> wrote in message
                news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Hello,
                >
                > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:[color=green]
                > > When I try this in my code I alwas get an errormessage: "Object[/color]
                > reference[color=green]
                > > not set to an instance of an object"
                > >
                > > Dim g As System.Drawing. Graphics
                > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,[/color]
                > Brushes.Black,[color=green]
                > > 0, 0)[/color]
                >
                > You never assign an instance of the Graphics class to the instance
                > variable g. Use something like this:
                >
                > \\\
                > Dim g As Graphics = Me.CreateGraphi cs()
                > ///
                >
                > or
                >
                > \\\
                > Dim bmp As Bitmap = ...
                > Dim g As Graphics = Graphics.FromIm age(bmp)
                > ///
                >
                > HTH,
                > Herfried K. Wagner
                > --
                > MVP · VB Classic, VB .NET
                > http://www.mvps.org/dotnet
                >
                >[/color]


                Comment

                • Marc

                  #9
                  Re: Error &quot;Object reference not set to an instance of an object&quot;

                  I'm a little bit further:

                  Module Procedures
                  Friend WithEvents PrintDocument1 As New
                  System.Drawing. Printing.PrintD ocument()
                  Dim bmp As New Bitmap(1, 1)
                  Dim g As Graphics = Graphics.FromIm age(bmp)

                  Sub Print()
                  g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in bericht
                  news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...[color=blue]
                  > Hello,
                  >
                  > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:[color=green]
                  > > When I try this in my code I alwas get an errormessage: "Object[/color]
                  > reference[color=green]
                  > > not set to an instance of an object"
                  > >
                  > > Dim g As System.Drawing. Graphics
                  > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,[/color]
                  > Brushes.Black,[color=green]
                  > > 0, 0)[/color]
                  >
                  > You never assign an instance of the Graphics class to the instance
                  > variable g. Use something like this:
                  >
                  > \\\
                  > Dim g As Graphics = Me.CreateGraphi cs()
                  > ///
                  >
                  > or
                  >
                  > \\\
                  > Dim bmp As Bitmap = ...
                  > Dim g As Graphics = Graphics.FromIm age(bmp)
                  > ///
                  >
                  > HTH,
                  > Herfried K. Wagner
                  > --
                  > MVP · VB Classic, VB .NET
                  > http://www.mvps.org/dotnet
                  >
                  >[/color]


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: Error &quot;Object reference not set to an instance of an object&quot;

                    Marc,
                    You are drawing on a bitmap, not on the document!

                    To draw on the document you need to handle the PrintDocument.P rintPage
                    event. This event has a PrintPageEventA rgs parameter. The PrintPageEventA rgs
                    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:



                    Hope this helps
                    Jay


                    "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                    news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...[color=blue]
                    > I'm a little bit further:
                    >
                    > Module Procedures
                    > Friend WithEvents PrintDocument1 As New
                    > System.Drawing. Printing.PrintD ocument()
                    > Dim bmp As New Bitmap(1, 1)
                    > Dim g As Graphics = Graphics.FromIm age(bmp)
                    >
                    > Sub Print()
                    > g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in bericht
                    > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...[color=green]
                    > > Hello,
                    > >
                    > > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:[color=darkred]
                    > > > When I try this in my code I alwas get an errormessage: "Object[/color]
                    > > reference[color=darkred]
                    > > > not set to an instance of an object"
                    > > >
                    > > > Dim g As System.Drawing. Graphics
                    > > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,[/color]
                    > > Brushes.Black,[color=darkred]
                    > > > 0, 0)[/color]
                    > >
                    > > You never assign an instance of the Graphics class to the instance
                    > > variable g. Use something like this:
                    > >
                    > > \\\
                    > > Dim g As Graphics = Me.CreateGraphi cs()
                    > > ///
                    > >
                    > > or
                    > >
                    > > \\\
                    > > Dim bmp As Bitmap = ...
                    > > Dim g As Graphics = Graphics.FromIm age(bmp)
                    > > ///
                    > >
                    > > HTH,
                    > > Herfried K. Wagner
                    > > --
                    > > MVP · VB Classic, VB .NET
                    > > http://www.mvps.org/dotnet
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Marc

                      #11
                      Re: Error &quot;Object reference not set to an instance of an object&quot;

                      At last, it prints.

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

                      I used this code:

                      Private WithEvents PrintDocument1 As New
                      System.Drawing. Printing.PrintD ocument()
                      Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                      System.Drawing. Printing.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
                      e.Graphics.Draw String("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.PrintP ageEventArgs, Tekst As String, x As Integer, y
                      As Integer) Handles PrintDocument1. PrintPage
                      e.Graphics.Draw String(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]" <Jay_Harlow@ema il.msn.com> schreef in
                      bericht news:%23deOrlxa DHA.652@tk2msft ngp13.phx.gbl.. .[color=blue]
                      > Marc,
                      > You are drawing on a bitmap, not on the document!
                      >
                      > To draw on the document you need to handle the PrintDocument.P rintPage
                      > event. This event has a PrintPageEventA rgs parameter. The[/color]
                      PrintPageEventA rgs[color=blue]
                      > object has a Graphics property. You need to draw on this Graphics object[/color]
                      to[color=blue]
                      > have it shown on the document you are attempting to print.
                      >
                      > For a reasonable complete sample of using PrintDocument see:
                      >[/color]
                      http://msdn.microsoft.com/library/de...et12242002.asp[color=blue]
                      >[/color]
                      http://msdn.microsoft.com/library/de...et01282003.asp[color=blue]
                      >
                      > Hope this helps
                      > Jay
                      >
                      >
                      > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                      > news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...[color=green]
                      > > I'm a little bit further:
                      > >
                      > > Module Procedures
                      > > Friend WithEvents PrintDocument1 As New
                      > > System.Drawing. Printing.PrintD ocument()
                      > > Dim bmp As New Bitmap(1, 1)
                      > > Dim g As Graphics = Graphics.FromIm age(bmp)
                      > >
                      > > Sub Print()
                      > > g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in bericht
                      > > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...[color=darkred]
                      > > > Hello,
                      > > >
                      > > > "Microsoft" <Marc.VanSchand evijl@_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("T est", 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.CreateGraphi cs()
                      > > > ///
                      > > >
                      > > > or
                      > > >
                      > > > \\\
                      > > > Dim bmp As Bitmap = ...
                      > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                      > > > ///
                      > > >
                      > > > HTH,
                      > > > Herfried K. Wagner
                      > > > --
                      > > > MVP · VB Classic, VB .NET
                      > > > http://www.mvps.org/dotnet
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • CJ Taylor

                        #12
                        Re: Error &quot;Object reference not set to an instance of an object&quot;

                        Are you talking about adding another handler that happens when printpage is
                        fireD? I'm confused.

                        "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                        news:uQWaxtxaDH A.2372@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > At last, it prints.
                        >
                        > But (of course :-) ) I have another question:
                        >
                        > I used this code:
                        >
                        > Private WithEvents PrintDocument1 As New
                        > System.Drawing. Printing.PrintD ocument()
                        > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                        > System.Drawing. Printing.PrintP ageEventArgs) Handles[/color]
                        PrintDocument1. PrintPage[color=blue]
                        > e.Graphics.Draw String("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.PrintP ageEventArgs, Tekst As String, x As Integer,[/color]
                        y[color=blue]
                        > As Integer) Handles PrintDocument1. PrintPage
                        > e.Graphics.Draw String(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]" <Jay_Harlow@ema il.msn.com> schreef in
                        > bericht news:%23deOrlxa DHA.652@tk2msft ngp13.phx.gbl.. .[color=green]
                        > > Marc,
                        > > You are drawing on a bitmap, not on the document!
                        > >
                        > > To draw on the document you need to handle the PrintDocument.P rintPage
                        > > event. This event has a PrintPageEventA rgs parameter. The[/color]
                        > PrintPageEventA rgs[color=green]
                        > > object has a Graphics property. You need to draw on this Graphics object[/color]
                        > to[color=green]
                        > > have it shown on the document you are attempting to print.
                        > >
                        > > For a reasonable complete sample of using PrintDocument see:
                        > >[/color]
                        >[/color]
                        http://msdn.microsoft.com/library/de...et12242002.asp[color=blue][color=green]
                        > >[/color]
                        >[/color]
                        http://msdn.microsoft.com/library/de...et01282003.asp[color=blue][color=green]
                        > >
                        > > Hope this helps
                        > > Jay
                        > >
                        > >
                        > > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                        > > news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...[color=darkred]
                        > > > I'm a little bit further:
                        > > >
                        > > > Module Procedures
                        > > > Friend WithEvents PrintDocument1 As New
                        > > > System.Drawing. Printing.PrintD ocument()
                        > > > Dim bmp As New Bitmap(1, 1)
                        > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                        > > >
                        > > > Sub Print()
                        > > > g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in[/color][/color][/color]
                        bericht[color=blue][color=green][color=darkred]
                        > > > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...
                        > > > > Hello,
                        > > > >
                        > > > > "Microsoft" <Marc.VanSchand evijl@_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("T est", 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.CreateGraphi cs()
                        > > > > ///
                        > > > >
                        > > > > or
                        > > > >
                        > > > > \\\
                        > > > > Dim bmp As Bitmap = ...
                        > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                        > > > > ///
                        > > > >
                        > > > > HTH,
                        > > > > Herfried K. Wagner
                        > > > > --
                        > > > > MVP · VB Classic, VB .NET
                        > > > > http://www.mvps.org/dotnet
                        > > > >
                        > > > >
                        > > >
                        > > >[/color]
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Marc

                          #13
                          Re: Error &quot;Object reference not set to an instance of an object&quot;

                          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.PrintP ageEventArgs) Handles PrintDocument1. PrintPage
                          e.Graphics.Draw String("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_P rintPage("Tekst ", 0, 10)

                          Eventhandler:
                          Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                          System.Drawing. Printing.PrintP ageEventArgs, cTekst As String, x As Integer,
                          y As Integer
                          e.Graphics.Draw String(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" <ctaylor@morton welding.com> schreef in bericht
                          news:eqKXcwxaDH A.2928@tk2msftn gp13.phx.gbl...[color=blue]
                          > Are you talking about adding another handler that happens when printpage[/color]
                          is[color=blue]
                          > fireD? I'm confused.
                          >
                          > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                          > news:uQWaxtxaDH A.2372@TK2MSFTN GP10.phx.gbl...[color=green]
                          > > At last, it prints.
                          > >
                          > > But (of course :-) ) I have another question:
                          > >
                          > > I used this code:
                          > >
                          > > Private WithEvents PrintDocument1 As New
                          > > System.Drawing. Printing.PrintD ocument()
                          > > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                          > > System.Drawing. Printing.PrintP ageEventArgs) Handles[/color]
                          > PrintDocument1. PrintPage[color=green]
                          > > e.Graphics.Draw String("Tekst", New Font("Arial", 12,[/color][/color]
                          FontStyle.Bold) ,[color=blue][color=green]
                          > > 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.PrintP ageEventArgs, Tekst As String, x As[/color][/color]
                          Integer,[color=blue]
                          > y[color=green]
                          > > As Integer) Handles PrintDocument1. PrintPage
                          > > e.Graphics.Draw String(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]" <Jay_Harlow@ema il.msn.com> schreef in
                          > > bericht news:%23deOrlxa DHA.652@tk2msft ngp13.phx.gbl.. .[color=darkred]
                          > > > Marc,
                          > > > You are drawing on a bitmap, not on the document!
                          > > >
                          > > > To draw on the document you need to handle the PrintDocument.P rintPage
                          > > > event. This event has a PrintPageEventA rgs parameter. The[/color]
                          > > PrintPageEventA rgs[color=darkred]
                          > > > object has a Graphics property. You need to draw on this Graphics[/color][/color][/color]
                          object[color=blue][color=green]
                          > > to[color=darkred]
                          > > > have it shown on the document you are attempting to print.
                          > > >
                          > > > For a reasonable complete sample of using PrintDocument see:
                          > > >[/color]
                          > >[/color]
                          >[/color]
                          http://msdn.microsoft.com/library/de...et12242002.asp[color=blue][color=green][color=darkred]
                          > > >[/color]
                          > >[/color]
                          >[/color]
                          http://msdn.microsoft.com/library/de...et01282003.asp[color=blue][color=green][color=darkred]
                          > > >
                          > > > Hope this helps
                          > > > Jay
                          > > >
                          > > >
                          > > > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                          > > > news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...
                          > > > > I'm a little bit further:
                          > > > >
                          > > > > Module Procedures
                          > > > > Friend WithEvents PrintDocument1 As New
                          > > > > System.Drawing. Printing.PrintD ocument()
                          > > > > Dim bmp As New Bitmap(1, 1)
                          > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                          > > > >
                          > > > > Sub Print()
                          > > > > g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in[/color][/color]
                          > bericht[color=green][color=darkred]
                          > > > > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...
                          > > > > > Hello,
                          > > > > >
                          > > > > > "Microsoft" <Marc.VanSchand evijl@_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("T est", 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.CreateGraphi cs()
                          > > > > > ///
                          > > > > >
                          > > > > > or
                          > > > > >
                          > > > > > \\\
                          > > > > > Dim bmp As Bitmap = ...
                          > > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                          > > > > > ///
                          > > > > >
                          > > > > > HTH,
                          > > > > > Herfried K. Wagner
                          > > > > > --
                          > > > > > MVP · VB Classic, VB .NET
                          > > > > > http://www.mvps.org/dotnet
                          > > > > >
                          > > > > >
                          > > > >
                          > > > >
                          > > >
                          > > >[/color]
                          > >
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          • CJ Taylor

                            #14
                            Re: Error &quot;Object reference not set to an instance of an object&quot;

                            If I undertand you right then use RaiseEvent or AddHandler.


                            "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                            news:%23TnMf4xa DHA.2336@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                            > 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.PrintP ageEventArgs) Handles[/color]
                            PrintDocument1. PrintPage[color=blue]
                            > e.Graphics.Draw String("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_P rintPage("Tekst ", 0, 10)
                            >
                            > Eventhandler:
                            > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                            > System.Drawing. Printing.PrintP ageEventArgs, cTekst As String, x As[/color]
                            Integer,[color=blue]
                            > y As Integer
                            > e.Graphics.Draw String(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" <ctaylor@morton welding.com> schreef in bericht
                            > news:eqKXcwxaDH A.2928@tk2msftn gp13.phx.gbl...[color=green]
                            > > Are you talking about adding another handler that happens when printpage[/color]
                            > is[color=green]
                            > > fireD? I'm confused.
                            > >
                            > > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                            > > news:uQWaxtxaDH A.2372@TK2MSFTN GP10.phx.gbl...[color=darkred]
                            > > > At last, it prints.
                            > > >
                            > > > But (of course :-) ) I have another question:
                            > > >
                            > > > I used this code:
                            > > >
                            > > > Private WithEvents PrintDocument1 As New
                            > > > System.Drawing. Printing.PrintD ocument()
                            > > > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                            > > > System.Drawing. Printing.PrintP ageEventArgs) Handles[/color]
                            > > PrintDocument1. PrintPage[color=darkred]
                            > > > e.Graphics.Draw String("Tekst", New Font("Arial", 12,[/color][/color]
                            > FontStyle.Bold) ,[color=green][color=darkred]
                            > > > Brushes.Black, 0, 0)
                            > > > End Sub
                            > > >
                            > > > How can I send printdata from outside the PrintPage eventhandler to[/color][/color][/color]
                            the[color=blue][color=green][color=darkred]
                            > > > eventhandler. Something like:
                            > > >
                            > > > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                            > > > System.Drawing. Printing.PrintP ageEventArgs, Tekst As String, x As[/color][/color]
                            > Integer,[color=green]
                            > > y[color=darkred]
                            > > > As Integer) Handles PrintDocument1. PrintPage
                            > > > e.Graphics.Draw String(Tekst, New Font("Arial", 12,[/color][/color][/color]
                            FontStyle.Bold) ,[color=blue][color=green][color=darkred]
                            > > > Brushes.Black, x, y)
                            > > > End Sub
                            > > >
                            > > > Of course, this desn't work...
                            > > >
                            > > > Marc
                            > > >
                            > > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> schreef in
                            > > > bericht news:%23deOrlxa DHA.652@tk2msft ngp13.phx.gbl.. .
                            > > > > Marc,
                            > > > > You are drawing on a bitmap, not on the document!
                            > > > >
                            > > > > To draw on the document you need to handle the[/color][/color][/color]
                            PrintDocument.P rintPage[color=blue][color=green][color=darkred]
                            > > > > event. This event has a PrintPageEventA rgs parameter. The
                            > > > PrintPageEventA rgs
                            > > > > object has a Graphics property. You need to draw on this Graphics[/color][/color]
                            > object[color=green][color=darkred]
                            > > > to
                            > > > > have it shown on the document you are attempting to print.
                            > > > >
                            > > > > For a reasonable complete sample of using PrintDocument see:
                            > > > >
                            > > >[/color]
                            > >[/color]
                            >[/color]
                            http://msdn.microsoft.com/library/de...et12242002.asp[color=blue][color=green][color=darkred]
                            > > > >
                            > > >[/color]
                            > >[/color]
                            >[/color]
                            http://msdn.microsoft.com/library/de...et01282003.asp[color=blue][color=green][color=darkred]
                            > > > >
                            > > > > Hope this helps
                            > > > > Jay
                            > > > >
                            > > > >
                            > > > > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                            > > > > news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...
                            > > > > > I'm a little bit further:
                            > > > > >
                            > > > > > Module Procedures
                            > > > > > Friend WithEvents PrintDocument1 As New
                            > > > > > System.Drawing. Printing.PrintD ocument()
                            > > > > > Dim bmp As New Bitmap(1, 1)
                            > > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                            > > > > >
                            > > > > > Sub Print()
                            > > > > > g.DrawString("T est", New Font("Arial", 12,[/color][/color][/color]
                            FontStyle.Bold) ,[color=blue][color=green][color=darkred]
                            > > > > > Brushes.Black, 0, 0)
                            > > > > > PrintDocument1. Print()
                            > > > > > End Sub
                            > > > > > End Module
                            > > > > >
                            > > > > > This prints a blank page, not the word "test"???
                            > > > > >
                            > > > > > Marc
                            > > > > >
                            > > > > > "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> schreef in[/color]
                            > > bericht[color=darkred]
                            > > > > > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...
                            > > > > > > Hello,
                            > > > > > >
                            > > > > > > "Microsoft" <Marc.VanSchand evijl@_nospam_. c-luma.com> schrieb:
                            > > > > > > > When I try this in my code I alwas get an errormessage:[/color][/color][/color]
                            "Object[color=blue][color=green][color=darkred]
                            > > > > > > reference
                            > > > > > > > not set to an instance of an object"
                            > > > > > > >
                            > > > > > > > Dim g As System.Drawing. Graphics
                            > > > > > > > g.DrawString("T est", New Font("Arial", 12, FontStyle.Bold) ,
                            > > > > > > Brushes.Black,
                            > > > > > > > 0, 0)
                            > > > > > >
                            > > > > > > You never assign an instance of the Graphics class to the[/color][/color][/color]
                            instance[color=blue][color=green][color=darkred]
                            > > > > > > variable g. Use something like this:
                            > > > > > >
                            > > > > > > \\\
                            > > > > > > Dim g As Graphics = Me.CreateGraphi cs()
                            > > > > > > ///
                            > > > > > >
                            > > > > > > or
                            > > > > > >
                            > > > > > > \\\
                            > > > > > > Dim bmp As Bitmap = ...
                            > > > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                            > > > > > > ///
                            > > > > > >
                            > > > > > > HTH,
                            > > > > > > Herfried K. Wagner
                            > > > > > > --
                            > > > > > > MVP · VB Classic, VB .NET
                            > > > > > > http://www.mvps.org/dotnet
                            > > > > > >
                            > > > > > >
                            > > > > >
                            > > > > >
                            > > > >
                            > > > >
                            > > >
                            > > >[/color]
                            > >
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: Error &quot;Object reference not set to an instance of an object&quot;

                              Marc,[color=blue]
                              > How can I send printdata from outside the PrintPage eventhandler to the
                              > eventhandler. Something like:[/color]
                              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 PrintPageEventA rgs 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 PrintableElemen t

                              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 PrintableElemen ts
                              Private m_elements As ArrayList


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

                              [color=blue]
                              > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                              > System.Drawing. Printing.PrintP ageEventArgs) Handles[/color]
                              PrintDocument1. PrintPage

                              For Each element As PrintableElemen t In m_elements
                              element.Draw(e. Graphics)
                              Next
                              [color=blue]
                              > End Sub[/color]

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

                              Hope this helps
                              Jay


                              "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                              news:uQWaxtxaDH A.2372@TK2MSFTN GP10.phx.gbl...[color=blue]
                              > At last, it prints.
                              >
                              > But (of course :-) ) I have another question:
                              >
                              > I used this code:
                              >
                              > Private WithEvents PrintDocument1 As New
                              > System.Drawing. Printing.PrintD ocument()
                              > Sub PrintDocument1_ PrintPage(ByVal sender As Object, ByVal e As
                              > System.Drawing. Printing.PrintP ageEventArgs) Handles[/color]
                              PrintDocument1. PrintPage[color=blue]
                              > e.Graphics.Draw String("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.PrintP ageEventArgs, Tekst As String, x As Integer,[/color]
                              y[color=blue]
                              > As Integer) Handles PrintDocument1. PrintPage
                              > e.Graphics.Draw String(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]" <Jay_Harlow@ema il.msn.com> schreef in
                              > bericht news:%23deOrlxa DHA.652@tk2msft ngp13.phx.gbl.. .[color=green]
                              > > Marc,
                              > > You are drawing on a bitmap, not on the document!
                              > >
                              > > To draw on the document you need to handle the PrintDocument.P rintPage
                              > > event. This event has a PrintPageEventA rgs parameter. The[/color]
                              > PrintPageEventA rgs[color=green]
                              > > object has a Graphics property. You need to draw on this Graphics object[/color]
                              > to[color=green]
                              > > have it shown on the document you are attempting to print.
                              > >
                              > > For a reasonable complete sample of using PrintDocument see:
                              > >[/color]
                              >[/color]
                              http://msdn.microsoft.com/library/de...et12242002.asp[color=blue][color=green]
                              > >[/color]
                              >[/color]
                              http://msdn.microsoft.com/library/de...et01282003.asp[color=blue][color=green]
                              > >
                              > > Hope this helps
                              > > Jay
                              > >
                              > >
                              > > "Marc" <Marc.VanSchand evijl@_nospam_. c-luma.com> wrote in message
                              > > news:uzVX9YxaDH A.1580@tk2msftn gp13.phx.gbl...[color=darkred]
                              > > > I'm a little bit further:
                              > > >
                              > > > Module Procedures
                              > > > Friend WithEvents PrintDocument1 As New
                              > > > System.Drawing. Printing.PrintD ocument()
                              > > > Dim bmp As New Bitmap(1, 1)
                              > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                              > > >
                              > > > Sub Print()
                              > > > g.DrawString("T est", 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]" <hirf.nosp@m.ac tivevb.de> schreef in[/color][/color][/color]
                              bericht[color=blue][color=green][color=darkred]
                              > > > news:uewbCrwaDH A.1740@TK2MSFTN GP10.phx.gbl...
                              > > > > Hello,
                              > > > >
                              > > > > "Microsoft" <Marc.VanSchand evijl@_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("T est", 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.CreateGraphi cs()
                              > > > > ///
                              > > > >
                              > > > > or
                              > > > >
                              > > > > \\\
                              > > > > Dim bmp As Bitmap = ...
                              > > > > Dim g As Graphics = Graphics.FromIm age(bmp)
                              > > > > ///
                              > > > >
                              > > > > HTH,
                              > > > > Herfried K. Wagner
                              > > > > --
                              > > > > MVP · VB Classic, VB .NET
                              > > > > http://www.mvps.org/dotnet
                              > > > >
                              > > > >
                              > > >
                              > > >[/color]
                              > >
                              > >[/color]
                              >
                              >[/color]


                              Comment

                              Working...