1 Attachment(s)
[RESOLVED] [02/03] String Manipulation and Printing
I'm trying to print the contents of a TextBox using PrintDocument
I'm manipulating the string in order to align it
The problem is that when it is printed out it is not aligned while when writing it to a text document everything looks in place
Please find attached my example
Thank You
Re: [02/03] String Manipulation and Printing
You'll find that many people, myself included, will rarely download zipped attachments. You should simply post your PrintPage event handler directly. You can include any other relevant code too but please none that isn't specifically relevant to the question. Otherwise many people will just not bother.
Re: [02/03] String Manipulation and Printing
thank you for clearing that out :thumb:
here is my code
Code:
Dim t As New DataTable
Dim r, r1, r2, dr As DataRow
Dim WithEvents pdoc As New Printing.PrintDocument
Dim str As String = ""
Function total1()
Try
Dim sum As Decimal = 0
Dim i As Integer
For i = 0 To t.Rows.Count - 1
sum = sum + Convert.ToDecimal(DataGrid1.Item(i, 2))
Next i
sum = (sum + ((TextBox2.Text / 100) * sum))
TextBox3.Text = sum
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Warning")
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
str = "Any Establishment" & vbCrLf & "Address: Street 100" & vbCrLf & "Telephone: 123456789" & vbCrLf & vbCrLf
str = str & "Item" & Space(50 - Len("Item")) & "Qty" & Space(20 - Len("Qty")) & "Price" & vbCrLf
str = str & "=======================================================" & vbCrLf
For Each dr In t.Rows
str = str & dr.Item(0) & Space(50 - Len(dr.Item(0))) & dr.Item(1) & Space(20 - Len(dr.Item(1))) & dr.Item(2) & vbCrLf
Next
str = str & "=======================================================" & vbCrLf
str = str & "TOTAL:" & Space(70 - Len("TOTAL:")) & TextBox3.Text
TextBox1.Text = str
pdoc.Print()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Warning")
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
r = t.NewRow()
r1 = t.NewRow()
r2 = t.NewRow()
t.Columns.Add("Item")
t.Columns.Add("Qty")
t.Columns.Add("Price")
r("Item") = "Item1"
r("Qty") = "2"
r("Price") = "500"
t.Rows.Add(r)
r1("Item") = "Item11"
r1("Qty") = "10"
r1("Price") = "7500"
t.Rows.Add(r1)
r2("Item") = "Item111"
r2("Qty") = "9"
r2("Price") = "20000"
t.Rows.Add(r2)
DataGrid1.DataSource = t
total1()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Warning")
End Try
End Sub
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Dim font As New Font("Times New Roman", 12)
e.Graphics.DrawString(str, font, Brushes.Black, 10, 10)
End Sub
Re: [02/03] String Manipulation and Printing
Use a fixed width font, such as Courrier.
Re: [02/03] String Manipulation and Printing
The string will not be aligned because you aren't specifying any alignment. Your just drawing the string at (10, 10) with the default top, left alignment. If you want anything other than the default you need to specify it. The DrawString method has numerous overloads so go to the MSDN library and read the documentation. Explore the different overloads and find one that allows you to specify the alignment of the text.
Re: [02/03] String Manipulation and Printing
Thank you works great :thumb: