Results 1 to 7 of 7

Thread: passing parameter to print

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    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

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

    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.
    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
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: passing parameter to print

    Quote Originally Posted by holisticsam View Post
    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...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: passing parameter to print

    Thanks. That solved my problem.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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