我一直在使用Visual编写作业,我对打印函数并不太熟悉,但我从参考书中了解了它们的工作原理。然而,问题似乎不像string.format()函数那样适用于打印函数。出于某种原因,它的行为是不理性的,例如,有一些字段相邻,另一些几个空格。我尝试过一些东西,如填充和使用单间隙字体,但即使是我也会遇到对齐问题。我知道标题根本没有格式化,我现在主要关心的是身体,因为这是动态的。数据是从一个列表框中提取出来的,该列表框以逗号删除的格式格式化,并且看起来很好地拆分成子字符串,但是当将数据放入数组然后打印预览时,很明显,格式并不像我想象的那样工作。我尝试过几种不同的方法,但都没有用。我相信如果我需要的话,我可以找出一个工作,但是这个函数看起来应该能工作,也许我遗漏了一些东西。如能在此问题上提供任何帮助,将不胜感激。

Private Sub mnu_file_print_Click(sender As Object, e As EventArgs) Handles mnu_file_print.Click
PrintDocument1.Print()
End Sub
Private Sub mnu_file_print_preview_Click(sender As Object, e As EventArgs) Handles mnu_file_print_preview.Click
PrintDocument1.DefaultPageSettings.Landscape = True
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim Record_Array(9) ' Array used to hold the for each word loop strings for inserting into the table.
Dim int_loop_num As Integer = 0
Dim str_print_body As String = String.Format("{0,-10}{1,-10}{2,-20}{3,-20}{4,-20}{5,-20}{6,-15}{7,-20}{8,-20}{9,-5}", Record_Array(0) & "|", Record_Array(1) & "|", Record_Array(2) & "|", Record_Array(3) & "|", Record_Array(4) & "|", Record_Array(5) & "|", Record_Array(6) & "|", Record_Array(7) & "|", Record_Array(8) & "|", Record_Array(9) & "|")
Dim startX As Integer = 62
Dim startY As Integer = 62
Dim font As New Font("Courier New", 7, FontStyle.Regular)
e.Graphics.DrawString("Room Number,Bench Number,Make,Model,Name,Serial Number,Device Description,Device Use,State Tag,Repair", lst_print_box.Font, Brushes.Black, startX, startY - 16)
' for each item in the list
For Each index As String In lst_print_box.Items
' for each sub string in the item
For Each word As String In Split(index, ",")
Record_Array(int_loop_num) = word.PadRight(5, "_").PadLeft(10, "_")
int_loop_num += 1
Next
str_print_body = String.Format("{0,-10}{1,-10}{2,-20}{3,-20}{4,-20}{5,-20}{6,-15}{7,-20}{8,-20}{9,-5}", Record_Array(0) & "|", Record_Array(1) & "|", Record_Array(2) & "|", Record_Array(3) & "|", Record_Array(4) & "|", Record_Array(5) & "|", Record_Array(6) & "|", Record_Array(7) & "|", Record_Array(8) & "|", Record_Array(9) & "|")
e.Graphics.DrawString(str_print_body, font, Brushes.Black, startX, startY)
int_loop_num = 0
startY += lst_print_box.ItemHeight
Next
End Sub发布于 2016-03-30 06:45:01
结果字符串中似乎有额外的空格。检查变量str_print_body的内容,以获得额外的空格。如果有的话,您可以裁剪每一个字符串,例如更改
从Record_Array(2)到Left(Record_Array(2), 20)
更好的办法是了解额外空间的来源,并直接解决问题的根源。
https://stackoverflow.com/questions/36300399
复制相似问题