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:
Dim a As String = Label1.Text & Label2.Text & textbox3.text & vbCrLf & _
& Label4.Text & Label5.Text
Is there any other best way to print a vb form contents....Let me know thanks..
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.
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 :lol:
..is there any easier way or better way to print the form.. ;) i dont want to print the screen shot :p
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.
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:
Protected Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim linesPerPage As Single = 0
Dim yPosition As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
'Dim printFont As Font = Label4.Font
Dim printFont As Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Dim myBrush As New SolidBrush(Color.Black)
' Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Iterate over the string using the StringReader, printing each line.
Dim a As String
line = myReader.ReadLine()
While count < linesPerPage ' And (Nothing <> line)
' calculate the next line position based on
' the height of the font according to the printing device
yPosition = (topMargin + (count * printFont.GetHeight(ev.Graphics)))
' draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat)
count += 1
line = myReader.ReadLine()
End While
' If there are more lines, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
myBrush.Dispose()
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?
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.
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..
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.