Results 1 to 18 of 18

Thread: Simple Text Printer in 2010

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Simple Text Printer in 2010

    In VB6, when I wanted to print text, I would do something like this...

    Code:
    Dim TextToPrint as string
    TextToPrint = LastName(1) & ", " & FirstName(1) & " " & G(1,1) & " " & G(1,2)  & " " & G(1,3) & vbCrLf
    TextToPrint = LastName(2) & ", " & FirstName(2) & " " & G(2,1) & " " & G(2,2)  & " " & G(2,3)
    TextToPrint = LastName(3) & ", " & FirstName(3) & " " & G(3,1) & " " & G(3,2)  & " " & G(3,3) & vbCrLf
    Printer.Print TextToPrint
    What's the equivalent in VB 2010?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Text Printer in 2010

    In VB.NET you print using a PrintDocument. Call its Print method and handle its PrintPage event. In the event handler, use GDI+ to draw whatever you want printed, e.g. DrawString to print text. It's a bit more complex for simple situations but it's far more powerful and flexible, so it can handle many more complex situations. Merrion has provided a beginners tutorial in the VB.NET CodeBank.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Simple Text Printer in 2010

    Use my Printer class. It's exactly what you need. The code is here:
    http://www.vbforums.com/showthread.php?t=608727

    Using it, all you need is to call:
    Code:
    My.Printer.Print(TextToPrint)

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Text Printer in 2010

    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    Pradeep, that code looks like it would work well for printing a file that already exists. But that's not what I want to do. I could make a file from my data, but that would be an extra step. I just want to print the data that's already in my app.

    I did come up with the code below but is still not quite what I was wanting.

    Code:
    Public Class Form1
        Dim PrintText As String = "Line 1"
    
    
        Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            PrintText = PrintText & vbCrLf
            PrintText = PrintText & "Second line" & vbCrLf
            PrintText = PrintText & "Line #3" & vbCrLf & vbCrLf
            PrintText = PrintText & "The line after the blank line."
            PrintDocument1.Print()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim x As Single
            Dim y As Single
    
            x = 10 'e.MarginBounds.Left
            y = 10 ' e.MarginBounds.Top
    
            Dim PrintFont As New Font("Arial", 10)
            e.Graphics.DrawString(PrintText, PrintFont, Brushes.Black, x, y)
    
        End Sub
    
    End Class
    I'd like to be able to change font size and color in certain areas of text. And I'd like to be able to draw grid lines around tabular data. Looks like I need a separate print line for each line of text to be printed.

    I'm still studying it and still looking for more suggestions.

  6. #6

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    cicatrix, I looked at it.

    What if I want to change font size or color in the middle of a sentence?

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Simple Text Printer in 2010

    Without messing with the Private Shared Sub PrintPageHandler this would be impossible. Generally you should change _myfont and Brush within this sub to modify the size/color correspondently.

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Text Printer in 2010

    Maybe something like this would do. Not tested though.

    vb.net Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.     Dim x As Single = 10
    3.     Dim y As Single = 10
    4.     Dim lineHeight As Integer = 10
    5.  
    6.     Dim font_Arial10 As New Font("Arial", 10)
    7.     Dim font_Verdana12B As New Font("Verdana", 12, FontStyle.Bold)
    8.  
    9.     e.Graphics.DrawString("First Line", font_Arial10, Brushes.Black, x, y)
    10.     e.Graphics.DrawString("Second Line", font_Arial10, Brushes.Red, x, y + lineHeight)
    11.     e.Graphics.DrawString("The line after the blank line.", font_Verdana12B, Brushes.Blue, x, y + lineHeight * 3)
    12.  
    13. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    Yeah, that works.

    You showed me how to get colors, sizes, and bold. Thanks.

    Can you tell me how to get a light gray filled rectangle around a row or printed text? I need it to go all the way across the page. I tried to get this working. Still searching on how to do it.

    Code:
    e.Graphics.DrawRectangle(Pens.AliceBlue,rect:= Rectangle(x,y,60,10)

    Also, how could I draw a thin line under a row of text, also all the way across the page?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Text Printer in 2010

    To fill a rectangle you call FillRectangle. To draw a line you call DrawLine. You can get things like page dimensions from the 'e' parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    Is that e.Graphics.FillRectangle and e.Graphics.DrawLine?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    I got the box working. Print the box, then print the text over it.

    I'm still confused on the whole printing thing though. My app needs to create several different kinds of printouts. When I call

    Code:
    PrintDocument1.Print()
    the PrintDocument1_PrintPage is called. Do I need a separate PrintDocument control for each kind of printout or what?

    My data will be coming from a DataGridView and will have student names and scores, averages, attendance, personal info, as well as info for assignments, etc.

    In VB6, I would just create a long string with vbCRLF items in it, and send that one string to the printer with Printer.Print. I could still do that but I want to add filledrectangles every few lines to make it easier to read.

    Any help/suggestions at all would be appreciated.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Text Printer in 2010

    You can print whatever GDI+ can draw, which is why .NET printing is more complex than VB6 printing: it's much more powerful. You just need to do some maths to work out where you want everything. It's not that hard but it's not trivial either.

    You might use multiple PrintDocuments or you might just use one. A PrintPage event handler is just a method like any other, so it can contain the same code as any other method. It can contain If and/or Select Case statements to do different things based on a condition.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Text Printer in 2010

    Here is one of the many ways to use the same PrintDocument object to print multiple types of pages.

    vb.net Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.     Select Case whatever
    3.         Case something
    4.             PrintMethod1(e)
    5.         Case somethingElse
    6.             PrintMethod2(e)
    7.  
    8.     End Select
    9. End Sub
    10.  
    11. Private Sub PrintMethod1(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    12.     Dim x As Single = 10
    13.     Dim y As Single = 10
    14.     Dim lineHeight As Integer = 10
    15.  
    16.     Dim font_Arial10 As New Font("Arial", 10)
    17.     Dim font_Verdana12B As New Font("Verdana", 12, FontStyle.Bold)
    18.  
    19.     e.Graphics.DrawString("First Line", font_Arial10, Brushes.Black, x, y)
    20.     e.Graphics.DrawString("Second Line", font_Arial10, Brushes.Red, x, y + lineHeight)
    21.     e.Graphics.DrawString("The line after the blank line.", font_Verdana12B, Brushes.Blue, x, y + lineHeight * 3)
    22. End Sub
    23.  
    24. Private Sub PrintMethod2(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    25.     'second method to print your text
    26. End Sub
    27.  
    28. Private Sub PrintMethod3(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    29.     'third method to print your text
    30. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Simple Text Printer in 2010

    That's helpful. I assumed it would be something like that. For the case statement condition, would you recommend a global variable, a form.Tag or what?

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple Text Printer in 2010

    The logical option is a private member variable. You need to access it in more than one method but you don't need to access it outside the class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Simple Text Printer in 2010

    That's right. A private member variable seems most logical, unless you have some odd requirement.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width