ADO DOT NET Posted December 20, 2006 Posted December 20, 2006 In VB6.0 I was using this: 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: 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:) Quote
ADO DOT NET Posted December 23, 2006 Author Posted December 23, 2006 I didn't know this is a hard question! [:D] Perhaps there should be someone here that tried to print in vb.net! Quote
Administrators PlausiblyDamp Posted December 23, 2006 Administrators Posted December 23, 2006 The code you posted looks ok - did you try it and see what the output looked like? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted December 23, 2006 Leaders Posted December 23, 2006 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. Quote [sIGPIC]e[/sIGPIC]
ADO DOT NET Posted December 26, 2006 Author Posted December 26, 2006 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? Quote
Administrators PlausiblyDamp Posted December 26, 2006 Administrators Posted December 26, 2006 (edited) 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. Edited December 26, 2006 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted December 26, 2006 Leaders Posted December 26, 2006 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. 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] _ )) [/Code] You just need to calculate and substitute the values shown in red. You already know the [i][color=Red]LeftMarginX[/color][/i] and [i][color=Red]TopMarginY[/color][/i], you just need to determine the space between the margins. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.