Hi Forum, Printing Question

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
In VB6.0 I was using this:
Visual Basic:
Printer.Print(TextBox1.Text)
To print the contents of a text box via a printer.
That was OK and simple!
But now in VB.NET 2005:
Visual Basic:
Private Sub PrintLogButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintLogButton.Click   
  
    Try  
        PrintDialog.Document = PrintDocument   
        If PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then  
            PrintDocument.Print()   
        End If  
    Catch Exception As Exception   
  
    End Try  
End Sub  
  
Private Sub PrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage   
    e.Graphics.DrawString(ReportTextBox.Text, New Font("Arial", 10, FontStyle.Regular), Brushes.Black, 20, 20)   
End Sub
Because I am new to .NET, I just want to know if I am doing everything allright?
I want to know if my code is based on a true procedure?
And it won't case an error to occur?
Thanks all the people:)
 
The best way to find out if something works is to try it. An important part of programming is testing and debugging. (If you are working on an application that prints, then you should be willing to sacrifice some ink and paper.) If you are worried that your code is not sound and may cause an error, the best thing to do would be to use that code and try out different scenarios. Use your program and try to cause an error. Think about possible problems such as what would happen if there is too much text to fit on a page, or special characters that might not print correctly, and try them out.
 
Hi,
Thank you for your great help:)
I found that there is one problem with my code:
The output line won't be word wrapped!
I mean if more than a specific caharcters, the rest won't be printed unless it reaches the 1st LineBreak in the text box.
How do you handle this case?
 
Edit : ignore me and read mable_eater's post instead.

You would need to use the MeasureString function provided by the graphics object to discover the height / width of the string - if it is wider than the available space you would need to draw part of the string and then draw the rest on the next line.
 
Last edited:
Actually, the DrawString method will properly wrap the string if you specify a RectangleF to draw the string in instead of only an x and y.
Code:
e.Graphics.DrawString(ReportTextBox.Text, _
    [COLOR="Blue"]New[/COLOR] Font("Arial", 10, FontStyle.Regular), _
    Brushes.Black, _
    [COLOR="Blue"]New[/COLOR] RectangleF( _
        [I][COLOR="Red"]LeftMarginX[/COLOR][/I], _
        [I][COLOR="Red"]TopMarginY[/COLOR][/I], _
        [I][COLOR="Red"]SpaceBetweenMarginsX[/COLOR][/I], _
        [I][COLOR="Red"]SpaceBetweenMarginsY[/COLOR][/I] _
    ))
You just need to calculate and substitute the values shown in red. You already know the LeftMarginX and TopMarginY, you just need to determine the space between the margins.
 
Back
Top