passing parameter to print
I'd like to eliminate storing the string in the TextBox2.Text and pass the parameter directly. How do I pass a parameter onto the Sub PrintDocument1_PrintPage? I'd like to do something like PrintDocument1.Print("test print")
********************
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
TextBox2.Text = "test print"
PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Create string to draw.
Dim drawString As [String] = TextBox2.Text
' Create font and brush.
Dim drawFont As New Font("Arial", 10)
Dim drawBrush As New SolidBrush(Color.Black)
' Create rectangle for drawing.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim width As Single = 200.0F
Dim height As Single = 50.0F
Dim drawRect As New RectangleF(x, y, width, height)
' Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect)
End Sub
Re: passing parameter to print
You don't pass anything to the PrintPage event handler. You call Print and the PrintDocument raises its PrintPage event, which you handle. the event handler then retrieves whatever data it needs. You don't have to use a TextBox though. What data type is the Text property of a TextBox? It's a String, so just use a String variable.
By the way, you don't have to create a SolidBrush yourself when it's a standard colour. The Brushes class has a property for each standard Color that returns a SolidBrush object. There's a corresponding Pens class for Pen objects. If you ever do create a Brush or a Pen though, make sure that you dispose it when you're done with it, as you must do with all objects that have a Dispose method. The proper way to do that is to create them with a using statement, in which case they are disposed implicitly at the End Using statement.
Re: passing parameter to print
So you're wanting to print the contents on textbox2.text without any special formatting? Take a look at this codebank submission by cicatrix.
Re: passing parameter to print
It's a string. This code with a local variable doesn't work. I get a syntax error that address_info is not defined.
***************
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim address_info As String = "test print"
PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' Create string to draw.
Dim drawString As [String] = address_info
' Create font and brush.
Dim drawFont As New Font("Arial", 10)
Dim drawBrush As New SolidBrush(Color.Black)
' Create rectangle for drawing.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim width As Single = 200.0F
Dim height As Single = 50.0F
Dim drawRect As New RectangleF(x, y, width, height)
' Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect)
End Sub
Re: passing parameter to print
Quote:
Originally Posted by
holisticsam
It's a string. This code with a local variable doesn't work. I get a syntax error that address_info is not defined.
Quick request... wrap your code in code tags. As for the error it's because you declare address_info in one sub and try to call it in another. If you want to access that variable wherever in the class, declare it at the form level:
Code:
Private address_info As String = "test print"
Private Sub Button8_Click...
Re: passing parameter to print
Thanks. That solved my problem.
Re: passing parameter to print
Great, just be sure to mark the thread resolved. To do so: go to thread tools at the top of this webpage -> Mark Thread as Resolved