locked
why is vb.net printing so difficult?

    Question

  • Hi,

    I have loads of VB6 apps I am trying to migrate to VB.net and the biggest problem I have is that printer.print has no equivalent in vb.net

    Can anyone tell me a simple way to print in dot net?

    Thanks

    Tuesday, December 27, 2005 2:01 PM

Answers

  • It's not, really: just different from VB6. Much nicer, and controllable.

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim prn As New
    Printing.PrintDocument
        ' Select the PDF Printer
        prn.PrinterSettings.PrinterName =
    "pdf995"
        ' Handle the page events
        AddHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
        ' Do the print (Printing handled by the print page handler)
        prn.Print()
        ' Remove the page handler
        RemoveHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
    End
    Sub
    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As
    Printing.PrintPageEventArgs)
        Static i As Integer
    = 0
        i = i + 1
        ' Print a line on this page
        args.Graphics.DrawString("Printed a Line on page " & i.ToString, New Font(Me
    .Font, FontStyle.Regular), Brushes.Black, 50, 50)
        ' Have we printed enough pages?
        If i >= 5
    Then
            ' Done all the pages
            i = 0
            args.HasMorePages =
    False
        Else
            ' Not finished, yet
            args.HasMorePages =
    True
       End
    If
    End Sub

    This'll print 5 pages with a line of text on each page. Each page is 'drawn' in the Page Event Handler, just as if it was a graphic canvas (the args.Graphics is the graphical canvas).

    That should point you in the right direction. Start looking at the Printing.PrintDocument namespace.

    Tuesday, December 27, 2005 3:19 PM

All replies

  • Impossible is about right.

    I just can't see what to type to make something come out on paper, and I have written some neat VB stuff (IMHO) in th epast but this dot net thing has me stumped. Am I missing something or is nobody printing anymore?

    Maybe we have finally reached the holy grail of the paperless office by MS removing the ability to print.

    Seriously though, can anyone point me at a print command?

    Tuesday, December 27, 2005 2:20 PM
  • It's not, really: just different from VB6. Much nicer, and controllable.

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim prn As New
    Printing.PrintDocument
        ' Select the PDF Printer
        prn.PrinterSettings.PrinterName =
    "pdf995"
        ' Handle the page events
        AddHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
        ' Do the print (Printing handled by the print page handler)
        prn.Print()
        ' Remove the page handler
        RemoveHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
    End
    Sub
    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As
    Printing.PrintPageEventArgs)
        Static i As Integer
    = 0
        i = i + 1
        ' Print a line on this page
        args.Graphics.DrawString("Printed a Line on page " & i.ToString, New Font(Me
    .Font, FontStyle.Regular), Brushes.Black, 50, 50)
        ' Have we printed enough pages?
        If i >= 5
    Then
            ' Done all the pages
            i = 0
            args.HasMorePages =
    False
        Else
            ' Not finished, yet
            args.HasMorePages =
    True
       End
    If
    End Sub

    This'll print 5 pages with a line of text on each page. Each page is 'drawn' in the Page Event Handler, just as if it was a graphic canvas (the args.Graphics is the graphical canvas).

    That should point you in the right direction. Start looking at the Printing.PrintDocument namespace.

    Tuesday, December 27, 2005 3:19 PM
  •  vbman wrote:

    Impossible is about right.

    I just can't see what to type to make something come out on paper, and I have written some neat VB stuff (IMHO) in th epast but this dot net thing has me stumped. Am I missing something or is nobody printing anymore?

    Maybe we have finally reached the holy grail of the paperless office by MS removing the ability to print.

    Seriously though, can anyone point me at a print command?

    Hi There

    If You want to Old Fashion and Pump out Hard Copy.... Shell Out $800 and Buy the Pro edition... Slam Your Hard Data in a Database and use the Report Writer To Kill Trees.....Is This Crystal Clear.....

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed suppression

    Tuesday, December 27, 2005 3:19 PM
  • Hi Stephen,

    Thanks for the useful and very educational answer. I think I can get it down to ten lines instead of the one in VB6, but I'll take your word for it that this is a better way.

    I have printed from my app, now I just need to figure out a replacement for all my old paintpicture and drawbox commands.

    This is a great example of what Forums are for.

    Tom

    Tuesday, December 27, 2005 3:37 PM
  • Hi There SJW

    Thanks A Lot......

    Here’ a Variation of the Same Theme.....Needs a Button1 on a Form.... It ain’t Pretty But It Works Here......

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim prn As New Printing.PrintDocument

    prn.PrinterSettings.PrinterName.ToString()

    AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler

    prn.Print()

    RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler

    End Sub

    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)

    args.Graphics.DrawString("HelloWorld With A Printer", New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125)

    End Sub

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed suppression

    Tuesday, December 27, 2005 5:12 PM
  • So.... I have to ask.....is there anyone who really sees this as an improvement over what we had previously? (printer.print strMyText)

     

    Tuesday, December 27, 2005 9:21 PM
  • Printing is difficult in VB.Net in my opinion, especially with multiple pages involving text , different fonts and sizes, images and working out where page breaks go.

    My.Printer looked terrific but it was axed from the beta versions, hopefully it will return one day.  I am off to buy the full Visual Studio product as its report control looks to be the answer for me. 

    Image posted at http://www.accessgp.com/reportexample.JPG - not my work.  I am running VB Express with the report viewer installed - I can look at them but cant create them.

     

    GS

    Wednesday, December 28, 2005 9:23 PM
  • Personally, from what I've played around with, I much prefer this new method to the VB 'classic' way of printing - it was always a pain in the neck to print anything other than text.
    Wednesday, December 28, 2005 11:22 PM
  • My greatest problem is working out where page breaks go when I have variable amounts of text with different font sizes to print and keeping track of what has been printed and what hasn't.  Still hope My.Printer comes back as this would make simple printing of text much easier.
    Thursday, December 29, 2005 4:24 AM
  • Hi There

    args.Graphics.DrawString("HelloWorld With A Printer", New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125)

    Is About Intuitive as a Princess Kissing a Frog..... They Don’t Do that Anymore...... The OOP Mission was Lost Somewhere...... Show me An OOP language with Statements like This and I’m Gone......

    Printer.Printline."Fish".........

    Diskdrive.Writetext."Dog","Cat"......

    Monitor.Display."Hello Real World".....

    Etc......

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed It’s All Rock And Roll"

    Thursday, December 29, 2005 11:41 AM
  • You still have to tell it where you want to print it, what font and what color: how would you address that? What about drawing images, graphics and charts?

    Regardless, the drawing functions are ther same for both printer ad screen (picture box, button, form, control, etc.) and eliminates a lot of the esoteric nature of Classic VB, as well as using the Win32 API (loading pens and brushes; destroying handles, except when it's a system brush, etc.).

    With that one line of code, you have an object, you are taking the graphical portion of that object (a canvas), and you are going to draw text on it. You tell it the text you want, the style, the color and where. Pretty straight forward, really. In one line, as well.

    As a RAD tool for graphical applications, classic VB was pretty poor to develop a viable application with the native VB code: the Win32 API was the way to go. Now we have a native environment with decent drawing functions. Sounds like a winner of a Rapid Application Development tool, and a great step up from classic VB.

    Thursday, December 29, 2005 1:24 PM
  • Hi There


    You still have to tell it where you want to print it, what font and what color: how would you address that? What about drawing images, graphics and charts?

    Printer.Printline."Fish"......... See all the ..... They are Properties.....

    Printer.Printline."Fish".Prop1.prop2.prpn......

    All statements in My.New.Language MUST be Henny Youngman’s.... This Language would be on TOP of the Existing VB Language As the Net.Framework is UNDER the Existing VB Language....... Which means One can Pick and Choose Cafeteria Style........ How about this One Liner.......

    For.i=1.prop1.prop2.propn.next

    etc....

    Regardless, the drawing functions are ther same for both printer ad screen (picture box, button, form, control, etc.) and eliminates a lot of the esoteric nature of Classic VB, as well as using the Win32 API (loading pens and brushes; destroying handles, except when it's a system brush, etc.).

    With that one line of code, you have an object, you are taking the graphical portion of that object (a canvas), and you are going to draw text on it. You tell it the text you want, the style, the color and where. Pretty straight forward, really. In one line, as well.

    You Write Well but I count more than One line of Code..... You might be able to unnest them and make one arcane line...... The new VB is Terrific.... The Trouble is It is no more than An Ugly C#.......

    As a RAD tool for graphical applications, classic VB was pretty poor to develop a viable application with the native VB code: the Win32 API was the way to go. Now we have a native environment with decent drawing functions. Sounds like a winner of a Rapid Application Development tool, and a great step up from classic VB.

    Up And Down.......

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed It’s All Rock And Roll"

    Thursday, December 29, 2005 2:48 PM
  • That ok with fixed strings "yada..yada.." but what to do when we have a neatly formated datagridview of say a balance statement and I need a print out of that....I am having a real hell now
    Thursday, December 29, 2005 2:59 PM
  • Have it your way, Billy.

     

    Thursday, December 29, 2005 6:19 PM
  •  Xne0 wrote:
    That ok with fixed strings "yada..yada.." but what to do when we have a neatly formated datagridview of say a balance statement and I need a print out of that....I am having a real hell now

    Off the top of my head, you could loop through each cell in the grid. Since it's already formatted, and you know the width/height of the paper to print on, you can space the cells proportionately and use the text/number formatting of each cell to print the actual contents.

    Then you could add some lines and graphics to pretty it up a bit.

    Thursday, December 29, 2005 6:24 PM
  •  SJWhiteley wrote:

    Have it your way, Billy.

     

    Hi There

    I go Way Back in Basic- Apples, Pets, Etc...This Incarnation of Basic Ain’t Your Father’s Oldsmobile.....

    Back then Grade School Kid’s could easily Program with Basic.... I doubt that is the Case Now... Alls I’m saying is that there was Something Lost and is Missed Dearly by a Lot of People(Not Me)..... I been away for a while and Came Back..... I am going to Learn This OOP VB or Die on the Keyboard...... I must say Your Printing Program Jumped me Up quite a few Rungs on the Ladder.... Like.CUBS.Maybe.Next.Year.....

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed It’s All Rock And Roll"


    Thursday, December 29, 2005 8:03 PM
  • I also learned BASIC way back and have not done a lot of programming since I was a kid. A little here and there as needed in my various jobs.

    I am blown away by how much I can accomplish with VB knowing very little about the language. I can create a very professional looking programming that really gets into heady stuff....like databases!!!!! Never could have done this in my younger days with BASIC, Forth and such. I can accomplish an aweful lot without really understanding most of what I am writing.

    That said, I realize that I can create a very nice, slick, professional looking program that may be full of bugs, and exceptions just waiting to come out to the unsuspecting user. I cant help but wonder if this will enable hacks like me to publish and sell programs that are really not well written??????Hmmmmmm!!!!

    Well, I am having a heck of a lot of fun creating professional looking apps. And for that I am greatful.

     

    Thursday, December 29, 2005 9:09 PM
  •  Carl In Milford wrote:

    I also learned BASIC way back and have not done a lot of programming since I was a kid. A little here and there as needed in my various jobs.

    I am blown away by how much I can accomplish with VB knowing very little about the language. I can create a very professional looking programming that really gets into heady stuff....like databases!!!!! Never could have done this in my younger days with BASIC, Forth and such. I can accomplish an aweful lot without really understanding most of what I am writing.

    That said, I realize that I can create a very nice, slick, professional looking program that may be full of bugs, and exceptions just waiting to come out to the unsuspecting user. I cant help but wonder if this will enable hacks like me to publish and sell programs that are really not well written??????Hmmmmmm!!!!

    Well, I am having a heck of a lot of fun creating professional looking apps. And for that I am greatful.

    Hi There

    Hey Carl How the Hell are You.... I Hope You’re Still Alive........

    I am blown away by how much I can accomplish with VB knowing very little about the language. I can create a very professional looking programming that really gets into heady stuff....like databases!!!!! Never could have done this in my younger days with BASIC, Forth and such. I can accomplish an awful lot without really understanding most of what I am writing.

    Hell I feel the same Way...... Modification Of the Database Tutorials In The Visual Basic Guided Tour Seems to me to be "Big Time" Salable.... I don’t "Really Understand what the Hell I am doing Either.... How the Hell Can A Guy Like Stevey Say something like this..... "Well it it’s Pretty Simple You Just create a Graphics Object And Paint on it’s Canvas".... God I am not There and Wish The Hell I Was......

    That said, I realize that I can create a very nice, slick, professional looking program that may be full of bugs, and exceptions just waiting to come out to the unsuspecting user. I cant help but wonder if this will enable hacks like me to publish and sell programs that are really not well written??????Hmmmmmm!!!!

    I wouldn’t Worry about It Too Much.... All these "Objects are Pretty Clean and as long as they are Dotted..... Together No Problem..... The Less Procedural Code you Write The Less Chance You Have Of Being Sued.... Hell we are pretty Close to Entitlements Anyway.......

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed It’s All Rock And Roll"

    Thursday, December 29, 2005 10:50 PM
  • Friday, December 30, 2005 1:59 AM
  • Google found this for me ...
     
    --------------------------------------------
     
    Re: My.Computer.Printers - Not showing...

    "Sergio Terenas" <sterenas@manquehue.net> schrieb:
    >My.Computer shows Audio, Clipboard, Clock, FileSystem, Keyboard, Mouse,
    >etc, Networks, >Ports, etc, etc but no "Printers".


    Simple-printing support has unfortunately been removed from 'My' in the
    release version.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B URL:http://classicvb.org/petition/
     
    ---------------------------------------------
     
    Accident or on purpose ...
     
    Happy new year all
     
    Jake
    Saturday, December 31, 2005 9:00 AM
  • Your information is very useful if one is printing a line at a time.  However, what if one is printing a mixture of single lines and long text that needs textwrapping followed by more single lines and then followed again by text that needs testwrapping.  While you are at it, let's not forget that we can't print off the bottom of the page.
    Monday, January 16, 2006 3:36 AM
  •  

    I have been reading everything I can find in the forums on printing.  There is lots of good info for printing plain text and printing a single line of formatted text.

    What I want to know is whether or not it is possible to print several pages of formatted text with varied formatting within a single line and have the page breaks be at the end of a word not in the middle of a word?

    If it is possible then I sure would like to see some sample code of how it is done.

    If it is not possible, then what is one supposed to use VB Express for?  I can't think of hardly any program I use that doesn't print.

    Thanks, Lowell

    Thursday, January 19, 2006 5:07 AM
  • Mr. Whiteley,

    You are correct that one line of code does a lot.  But what it doesn't do is allow you to print a 3 page document with the 4th word in the second paragraph being bold and the 13th word in the fifth paragraph being underlined.

    I think a lot of people are frustrated with the new VB because they can't figure out how to print multi-page documents with formatting.

    I'm hoping someone will provide a code sample (if it can be done).

    Thanks, Lowell

    Monday, January 23, 2006 8:38 PM
  • I suspect you have seen code like http://msdn2.microsoft.com/en-us/library/7d29f66t.aspx to print a rich text box but that is not what you want.

    I can recommend Visual Basic 2005 Programmer's Reference by Rod Stephens for the best discussion and code for printing I have seen http://www.vb-helper.com/vb_prog_ref.htm.  I have bought an awful lot of VB texts and most gloss over printing because it is too hard. This one is good. With some simple modification of his routines I can get formating down to the paragraph level (position, font type and size) with page numbers at the top, headers left and right and multipage printing and print preview. If you were to treat a word as a formatted paragraph it might do what you want but a bit messy.

    I too am looking for finer control of printing but I can find no examples of this around.  I guess this is why so much time and money is spent on developing word processors.

    Now if anyone has seen an example of a Wordpad or Notepad replacement written in VB 2005 (or 2003) with source I would be very interested.

    GS

    Monday, January 23, 2006 10:16 PM
  • All the commands are available in .NET for fine control of the printing: you just have to use them.

    Having the capability to print like Word, Internet Explorer or Wordpad is not a trivial task, and will never be. The code to print will always be complex but the tools are there. Additionally, just because some of these tools are free (IE and Wordpad) doesn't imply easy to do.

    Whatever lanuage you choose, it will be 'hard'. It's unfair to critique VB as having poor printing capability, when, in fact it has excellent support. One of the hardest parts is how would you you 'tell' such a printing routine what the format is? What is bold? what portions are which font? (HTML comes to mind). Building the editor/descriptor is the first challenge.

    Start off with a fixed font size for the whole document and build a routine to print that, with appropriate margins, page breaks, etc. Then move on to defining bold and italic regions, as well as color regions. Once you have mastered those techniques move on to multiple font sizes for individual paragraphs, then varying font sizes for individual words.

    The nice thing about VB is that if you can build an editor, the printing is almost automatic - only the margins and page breaks will need defining.

    There isn't a magic bullet. Also, since there are many, many editors around (some free and some not so free), the incentive to produce an editor/printing routine is diminished.

    Tuesday, January 24, 2006 1:37 PM
  • I'm not doubting that all the commands exist in .NET, but from the number of people who seem to be having difficulty it would seem that no one can find them or figure them out.

    You state that having the "capability to print like Word, Internet Explorer or Wordpad is not a trivial task" and I don't doubt that.  However, in VB5 it wasn't a big deal to print something like the below:


    Everyone understands the importance of a healthy heart, but maintaining one can prove difficult.  Diet, genetics, a sedentary lifestyle, smoking, and an array of other factors can compromise cardio-vascular health.  Moreover, our bodies are often deficient in substances critical to healthy heart function.  One example is Coenzyme Q10 (CoQ10), which helps produce energy in each cell, particularly those in the heart and other major organs.  Since it has powerful antioxidant properties, it helps prevent LDL cholesterol oxidation.

     

    Although the human body can synthesize CoQ10, in some situations the body's capacity to produce CoQ10 isn't sufficient to meet its needs.  CoQ10 levels reach their peak in the human body by age 20 and fall slowly thereafter.  Our internal ability to produce CoQ10 declines with age, and as we get older, we must rely more on our diet.  In fact, 60% of the population has CoQ10 deficiency in their 50s and 60s.  In addition, certain medications can reduce the body's production of Coenzyme Q 10.  Since most people do not eat the perfect diet we must rely on supplementation to ensure wellness, but since most supplemental CoQ10 is poorly absorbed by the body, a supplement must be highly bioavailable to be effective.


    This could be done because it was possible to get the "pen position" at the end of a print segment, and it was possible to start the next print segment from the previous "pen position".  It was also possible to determine how much text space was available in a line from any given "pen position".  One could then write code to stop printing on a particular line at the last "space" character.  Similar logic would be used to determine when to stop printing on a page and start a new page.

     

    I wonder about the ease of printing in .NET when downloaded code sample truncate words at the end of a page of uniformly formatted text.

     

    If printing like that shown above can be done in .NET then I would certainly like to see a code sample of it.

     

    Thanks, Lowell

    Wednesday, January 25, 2006 5:59 PM
  • All you need to do is look up the functions associated with drawing text on a graphics canvas. It's not hard, just different from VB Classic. Here's a function I knocked out in a few minutes:

    Private Sub PrintParagraph(ByVal strParagraph As String, ByRef g As Graphics)
        Dim f As New Font("Verdana"
    , 24)
        Dim i As
    Integer
        Dim pos As New
    Point(0, 0)
        Dim col As New
    Color
        Dim WordSize As New
    SizeF
        Dim s() As String = Split(strParagraph, " "
    )
        For i = 0 To
    UBound(s)
            col = Color.FromArgb(CInt(Rnd() * 255), CInt(Rnd() * 255), CInt
    (Rnd() * 255))
            WordSize = g.MeasureString(s(i), f)
            If (WordSize.Width + pos.X) > 850
    Then
                pos.X = 0
                pos.Y = pos.Y + CInt
    (WordSize.Height)
            End
    If
            g.DrawString(s(i), f, New SolidBrush(col), pos.X, pos.Y)
            pos.X = pos.X + CInt
    (WordSize.Width)
        Next
    End Sub

    The code is a bit crappy, but it'll print a paragraph of text with each word in a different color. Basically, I made a lot of use of the F1 key. But that should get you going if that's what you are after, you should hve absolutely no problems whatsoever replicating what you have.

    Wednesday, January 25, 2006 9:04 PM
  • Hello  S.J.

    Appreciate your coding----I am a newbie also a retiree. Learning vb.net as a hobby.

    Can you kindly give a code  example using your function? I wrote some code using your stringname with a string of letters to print. Could not get past the "g as graphics" passed  to your function. Can You help on this one it would be much appreciated.

    Regards Cannon 

    Friday, January 27, 2006 5:21 AM
  • Sure. Essentially, if you read this entire thread (ugh!) you'll find enough code, but to condense it a little bit:

    First, we need to setup a handler for printing, and select a printer. Here, it's set up in a button:

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim prn As New
    Printing.PrintDocument
        '---- Select the PDF Printer
        prn.PrinterSettings.PrinterName =
    "pdf995"
        '---- Handle the page events
        AddHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
        '---- Do the print (Printing handled by the print page handler)
        prn.Print()
        '---- Remove the page handler
        RemoveHandler prn.PrintPage, AddressOf Me
    .PrintPageHandler
    End Sub

    Next, we have our handler outine, which handles the printing of each page. This is where the PrintParagraph() routine is called:

    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
        Dim str As
    String
        str =
    "This is a paragraph of text which we want to draw with bold and a rectangle "
        str = str &
    " around certain words with some italics"
        Debug.WriteLine(args.PageBounds.Size)
        Call
    PrintParagraph(str, args.Graphics)
    End Sub

    This whole process demonstrates the concept. hope that helps.

    Friday, January 27, 2006 1:09 PM
  • stephen

    Thanks for your prompt reply. Now makes sense I will give it a run.

    Regards

    Cannon

    Friday, January 27, 2006 1:41 PM
  •  vbman wrote:

    Hi,

    I have loads of VB6 apps I am trying to migrate to VB.net and the biggest problem I have is that printer.print has no equivalent in vb.net

    Can anyone tell me a simple way to print in dot net?

    Thanks

    Friday, January 27, 2006 8:23 PM
  •  Bronco Billy wrote:

    Hi There SJW

    Thanks A Lot......

    Here’ a Variation of the Same Theme.....Needs a Button1 on a Form.... It ain’t Pretty But It Works Here......

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim prn As New Printing.PrintDocument

    prn.PrinterSettings.PrinterName.ToString()

    AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler

    prn.Print()

    RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler

    End Sub

    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)

    args.Graphics.DrawString("HelloWorld With A Printer", New Font("Arial", 20, FontStyle.Bold), Brushes.Black, 150, 125)

    End Sub

    Cheers
    Bronco Billy
    "Beer and Fast Women are Ok... No Cigarettes or Hard Liquor Allowed suppression

    Friday, January 27, 2006 8:27 PM
  •  SJWhiteley wrote:

    All you need to do is look up the functions associated with drawing text on a graphics canvas. It's not hard, just different from VB Classic. Here's a function I knocked out in a few minutes:

    Private Sub PrintParagraph(ByVal strParagraph As String, ByRef g As Graphics)
        Dim f As New Font("Verdana"
    , 24)
        Dim i As
    Integer
        Dim pos As New
    Point(0, 0)
        Dim col As New
    Color
        Dim WordSize As New
    SizeF
        Dim s() As String = Split(strParagraph, " "
    )
        For i = 0 To
    UBound(s)
            col = Color.FromArgb(CInt(Rnd() * 255), CInt(Rnd() * 255), CInt
    (Rnd() * 255))
            WordSize = g.MeasureString(s(i), f)
            If (WordSize.Width + pos.X) > 850
    Then
                pos.X = 0
                pos.Y = pos.Y + CInt
    (WordSize.Height)
            End
    If
            g.DrawString(s(i), f, New SolidBrush(col), pos.X, pos.Y)
            pos.X = pos.X + CInt
    (WordSize.Width)
        Next
    End Sub

    The code is a bit crappy, but it'll print a paragraph of text with each word in a different color. Basically, I made a lot of use of the F1 key. But that should get you going if that's what you are after, you should hve absolutely no problems whatsoever replicating what you have.

    Friday, January 27, 2006 8:36 PM
  • i have researched thoroughly the new .net printing paradigm. However, the use of Graphics eliminates the ability to print directly to the printer using Internal Printer Fonts. Eg. i need a way to print using printer font "Stripe 2" such that the printer can print on the magnetic side. In VB6 this was very easy as the printer font's were easy enum'ed. Does anyone know of a way to print text directly to the printer specifying a Printer Font? (in .net of course)
    Saturday, January 28, 2006 12:18 PM
  • Thanks.  That gives me something to work with.  I've modified your code and have been able to print any word in any FontStyle or color I wish.  I don't know if I ever would have discovered this.  Sometimes it is impossible to get anywhere if you don't have a starting point.  You provided that starting point.  I've used the F1 key a lot since you posted this.  Before I didn't even know what to look for in Help.

    Thanks again, Lowell

    Tuesday, January 31, 2006 3:37 AM
  • is there anyone here help me to print the document located in the datagrid?hope you help me ...

     

    Wednesday, February 01, 2006 3:17 AM
  •  Archie_ojt wrote:

    is there anyone here help me to print the document located in the datagrid?hope you help me ...

     

    There are third-party derived DataGrids around that support printing themselves, plus there are third-party helper classes around that allow you to print the contents of a standard DataGrid.  Here is one such helper class.
    Wednesday, February 01, 2006 10:48 AM
  • Hi

    Have you found any kind of solution to this very irritating issue. I am building a cash register and need to print our reciepts to the clients with a barcode. This barcode font is stored inside the printer and they were kind enough to include VB 6 sample code but not to the .NET version and Visual Studio where not able to upgrade the project.

    Please let me know if there is a solution to this.

     

    Tuesday, February 07, 2006 6:48 PM
  • Hi,

    Can you help with this please:

    Hi,

    I am currenlty looking at the print process. The only process that fails to work is the forms Print Preview control.

    As per the MS documents I have read, all I need to do is simply provide it with a reference to the PrintDocument object.  When run it fails to display the document?

    What could be the cause of this? I have found other posts with the same issue but no answer.

    Any help would be appreciated as this is the only issue I have with the VB print process.

    Private Sub btnPreviewControl_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles btnPreviewControl.Click

    PrintPreviewControl1.Document = mDoc

    End Sub

     

    Ron Nash

    Tuesday, February 28, 2006 8:28 AM
  • Hi & Sorry,

    I'm to lazy, to read all the follow ups! I just wanted to add some sidenodes!

    1) Printing in VB(E) 2005 isn't so difficulty, it simply different than it was in VB6!

    2) Have a look at www.gotdotnet.com look in the "User Samples" for a sample "ReportCreator" or maybe "ReportGenerator" or so. It is as far as I remember, all you need for a simple Printing approach in .Net. It comes with Source Code, a Report Designer, and looked pretty good and reliable! And its free to use! I added it to my DotNet Knowledge/Sample DB. In case I need printing, again!

    Hope that helps,
       BavBoy
    Tuesday, February 28, 2006 2:35 PM
  • Before the SUB there are some lines like this:

    Imports System.IO

    Imports System.Drawing.Printing

    Public Class Form1

    Private printFont As Font

    Private streamToPrint As StreamReader

    Dim pd As New PrintDocument

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ..............

    See forum - James to Simeao:

    Thread Starter: Trucker   Started: 24 Feb 2006 6:05 PM UTC

    Monday, March 06, 2006 11:00 PM
  • I agree that the printing is difficult. Fortunately, I bought "Visual Basic 2005 Step by Step", which deals with printing, wrapping, measuring strings, previewdialog etc. etc. Tricky stuff if you don't have examples to observe...

    I'm still not quite sure how to print a line of text AND a graphic on one page. It seems that after the first print command is issued, the printer would grab the paper and start working. The graphic would then be on the second page(?) But I'll be looking into that tomorrow, using the online help and so on (sometimes I hate having to go trough the MSDN classes and members one by one, but on the other hand, it's a great way to learn. You're always bound to run into something new that you can try out).

    I'm sure that more and more 2005 samples and examples will become available on the internet and printing is bound to be discussed in loads of those.

    Wednesday, March 08, 2006 9:14 PM
  • After cracking open my VB2005 Step by Step book and using a few other rescources, I have finished my check printing application and I have to say, printing is REALLY NOT THAT BAD.  At first it may seem a little bit counterintuitive, but once you see how much power you have when printing a document, you begin to realize why it can be a little bit difficult at times.  Just stick with it and (if you're like me) keep that ruler handy :-)

    PS: Never listen to the user BroncoBilly.  Since I have been on this forum I have yet to see him make an actual contribution to any thread.
    Thursday, March 09, 2006 3:52 PM
  • Lydon,

    I am just starting the process of converting check printing app written in FoxPro to VB '05. I'd love to see how you went about doing it. The print file gets picked up by an application that maps certain fields to be printed on the magnetic Ink check printer. So the text has to lign up perfectly or the app that prints the checks gets fubarred. Reporting services and Crystal seems to get out of alignment depending onthe pc that prints the check.

    If you could give me a start I'd appreciate it.

    Thanks,
    Phil

    Thursday, August 10, 2006 2:26 PM
  • This code does not work for me.  I get all kinds of squigly lines and I am not sure what to do to print.  These forums are so advanced, is there anything for dummies?  How do I print, from start to finish?  What do I need to do with a Text File or Word Document, or what ever to design a report?
    Saturday, December 02, 2006 10:30 PM
  • Did you try that link you posted?? It says content not found.  This is what I was talking about earlier, the internet is full of junk, and useless resources, and you spend more time finding what you need because people in forums say "Look it up".
    Saturday, December 02, 2006 10:38 PM