|
-
May 13th, 2010, 08:36 PM
#1
Thread Starter
Hyperactive Member
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?
-
May 13th, 2010, 09:28 PM
#2
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.
-
May 14th, 2010, 01:37 AM
#3
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)
-
May 14th, 2010, 02:08 AM
#4
Re: Simple Text Printer in 2010
-
May 14th, 2010, 02:46 PM
#5
Thread Starter
Hyperactive Member
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.
-
May 14th, 2010, 02:51 PM
#6
Re: Simple Text Printer in 2010
Follow the link in my post #3:
http://www.vbforums.com/showthread.php?t=608727
Just add the code from there into your application and use:
Code:
My.Printer.Print("Hello")
-
May 14th, 2010, 04:22 PM
#7
Thread Starter
Hyperactive Member
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?
-
May 14th, 2010, 07:22 PM
#8
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.
-
May 15th, 2010, 01:30 PM
#9
Re: Simple Text Printer in 2010
Maybe something like this would do. Not tested though.
vb.net Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim x As Single = 10 Dim y As Single = 10 Dim lineHeight As Integer = 10 Dim font_Arial10 As New Font("Arial", 10) Dim font_Verdana12B As New Font("Verdana", 12, FontStyle.Bold) e.Graphics.DrawString("First Line", font_Arial10, Brushes.Black, x, y) e.Graphics.DrawString("Second Line", font_Arial10, Brushes.Red, x, y + lineHeight) e.Graphics.DrawString("The line after the blank line.", font_Verdana12B, Brushes.Blue, x, y + lineHeight * 3) End Sub
-
May 15th, 2010, 01:59 PM
#10
Thread Starter
Hyperactive Member
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?
-
May 15th, 2010, 11:43 PM
#11
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.
-
May 16th, 2010, 10:33 AM
#12
Thread Starter
Hyperactive Member
Re: Simple Text Printer in 2010
Is that e.Graphics.FillRectangle and e.Graphics.DrawLine?
-
May 16th, 2010, 12:55 PM
#13
Thread Starter
Hyperactive Member
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.
-
May 16th, 2010, 06:04 PM
#14
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.
-
May 17th, 2010, 04:12 AM
#15
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:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Select Case whatever Case something PrintMethod1(e) Case somethingElse PrintMethod2(e) End Select End Sub Private Sub PrintMethod1(ByVal e As System.Drawing.Printing.PrintPageEventArgs) Dim x As Single = 10 Dim y As Single = 10 Dim lineHeight As Integer = 10 Dim font_Arial10 As New Font("Arial", 10) Dim font_Verdana12B As New Font("Verdana", 12, FontStyle.Bold) e.Graphics.DrawString("First Line", font_Arial10, Brushes.Black, x, y) e.Graphics.DrawString("Second Line", font_Arial10, Brushes.Red, x, y + lineHeight) e.Graphics.DrawString("The line after the blank line.", font_Verdana12B, Brushes.Blue, x, y + lineHeight * 3) End Sub Private Sub PrintMethod2(ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'second method to print your text End Sub Private Sub PrintMethod3(ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'third method to print your text End Sub
-
May 17th, 2010, 11:35 AM
#16
Thread Starter
Hyperactive Member
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?
-
May 17th, 2010, 06:35 PM
#17
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.
-
May 18th, 2010, 06:47 AM
#18
Re: Simple Text Printer in 2010
That's right. A private member variable seems most logical, unless you have some odd requirement.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|