Results 1 to 8 of 8

Thread: Printing Vb Form contents

  1. #1

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Printing Vb Form contents

    Hi,
    I tried to print out a vb form. for that i followed the sample code..but instead of passing richtext box value i passed a string..in that string i append all the form variables what i've to print
    for example,
    VB Code:
    1. Dim a As String = Label1.Text & Label2.Text & textbox3.text & vbCrLf & _
    2.                             & Label4.Text & Label5.Text
    Is there any other best way to print a vb form contents....Let me know thanks..
    Think Before Ink

    Visual Studio .NET 2002/.NET Framework 1.0

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

    Re: Printing Vb Form contents

    If you want to print an image of the form then you can use API functions to get a screenshot of the form and print it as an image. If you want to print specific data from the form, then it is up to you to format it as you want and print it using the PrintDocument class and its PrintPage event.
    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

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: Printing Vb Form contents

    Thanks...i tried. to put all the variable in one stirng & print it using the print doc...but i found the formatting needs more work
    ..is there any easier way or better way to print the form.. i dont want to print the screen shot
    Think Before Ink

    Visual Studio .NET 2002/.NET Framework 1.0

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

    Re: Printing Vb Form contents

    You need to use the PrintDocument but you don't have to just put every variable into one string. You can print as many seperate strings, and other elements, as you want in the PrintPage event handler, and therefore format and position each part exactly as you want.
    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

  5. #5

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: Printing Vb Form contents

    oh thats great!!
    But..I'm doing as follows..can u give any suggestions for this??
    [VBCODE} myReader = New StringReader(strText)
    If PrintDialog1.ShowDialog() = DialogResult.OK Then
    Me.PrintDocument1.Print()
    End If[/Highlight]
    where strText is the sting to be print
    VB Code:
    1. Protected Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim linesPerPage As Single = 0
    3.         Dim yPosition As Single = 0
    4.         Dim count As Integer = 0
    5.         Dim leftMargin As Single = ev.MarginBounds.Left
    6.         Dim topMargin As Single = ev.MarginBounds.Top
    7.         Dim line As String = Nothing
    8.         'Dim printFont As Font = Label4.Font
    9.         Dim printFont As Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    10.         Dim myBrush As New SolidBrush(Color.Black)
    11.  
    12.         ' Work out the number of lines per page, using the MarginBounds.
    13.         linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
    14.  
    15.         ' Iterate over the string using the StringReader, printing each line.
    16.         Dim a As String
    17.         line = myReader.ReadLine()
    18.  
    19.         While count < linesPerPage ' And (Nothing <> line)
    20.  
    21.             ' calculate the next line position based on
    22.             ' the height of the font according to the printing device
    23.             yPosition = (topMargin + (count * printFont.GetHeight(ev.Graphics)))
    24.  
    25.             ' draw the next line in the rich edit control
    26.             ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat)
    27.             count += 1
    28.             line = myReader.ReadLine()
    29.         End While
    30.  
    31.         ' If there are more lines, print another page.
    32.         If Not (line Is Nothing) Then
    33.             ev.HasMorePages = True
    34.         Else
    35.             ev.HasMorePages = False
    36.         End If
    37.         myBrush.Dispose()
    38.     End Sub

    In this code i'm reading the string line by line but if i'm storing it in different string variables as you told means then how can I pass them?
    Think Before Ink

    Visual Studio .NET 2002/.NET Framework 1.0

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

    Re: Printing Vb Form contents

    Any variables that you want to print must be visible from the PrintPage event handler. This means that they must have class-level scope or greater. You can either place the desired values in class-level variables when the Print button is pressed or do it in the PrintPage event handler. Any variables you can be seen from the Button's Click event handler must already be able to be seen from the PrintPage event handler, other than those declared locally within it.
    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

  7. #7

    Thread Starter
    Addicted Member haihems's Avatar
    Join Date
    Oct 2004
    Posts
    150

    Re: Printing Vb Form contents

    Sorry jmcilhinney..i couldn't understand what u r saying..actually, the first three lines of my coding in the above reply is what i'm having in my print_click event.the rest are the printdocument_printpage coding...in this printpage event, i pass the string to the "stringReader" and read them line by line and print as so...but if I stroe the text (what I want to print) in different variable instead of one variable,how can I pass it to the "line" variable and how can I print them??? Hope you understand what I mean..(i've only less explanatory skill)..if u can't understand, i'll again explain..thanks anyway..
    Think Before Ink

    Visual Studio .NET 2002/.NET Framework 1.0

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

    Re: Printing Vb Form contents

    You wrote in your original post that you were simply putting all the Labels' Text properties into a single string. You can just as easily print individual Labels' Text properties from the PrintPage event handler. If you need to keep track of what has already been printed between calls to the PrintPage event handler, like a page number, you can do this with class level-variables or a Static variable declared within the event handler itself. Local variables declared as Static in VB are still only visible within the method in which they are declared, but they retain their value between calls.
    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

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