String.format问题

时间:2016-03-30 05:28:11

标签: vb.net string string.format

我一直在使用Visual Basic进行作业,我对打印功能并不太熟悉,但我从参考书中了解得足以了解它们的工作原理。然而,问题似乎与print.format()函数一样,与print函数无关。出于某种原因,它行为不合理,例如,彼此相邻的一些字段和其他几个空格。我尝试了一些诸如填充和使用等宽字体之类的东西,但即使我仍然遇到对齐问题。我知道标题根本没有格式化,我现在主要关注的是身体,因为它更加动态。数据是从以逗号分隔的样式格式化的列表框中提取的,并且似乎可以很好地分割成子字符串,但是当放入数组然后打印预览时,显然格式化不正常,因为我认为将。我尝试过几种不同的方法,但无济于事。我确定如果需要,我可以找出一个可以解决的问题,但似乎这个功能应该起作用,也许还有我想要的东西。任何有关此事的帮助将不胜感激。

enter image description here

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

1 个答案:

答案 0 :(得分:1)

结果字符串中似乎有多余的空格。 检查变量<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script> <div ng-app="my-app"> <div ng-controller="demoController"> <div ng-repeat="classified in classifieds"> <h1>{{classified.name }}</h1> <ul> <li ng-repeat="thought in classified.thoughts"> {{ thought }} <button ng-click="remove(classified, $index)">Remove</button> <button ng-click="edit(classified, $index, editor)">Edit</button> </li> </ul> <input ng-init="editor={index:-1}" type="text" ng-model="editor.input"> <button type="submit" ng-if="editor.index==-1" ng-click="save(classified, editor)">Save</button> <button type="submit" ng-if="editor.index>-1" ng-click="saveEdit(classified, editor)">Save Edit</button> </div> </div> </div>的内容是否有额外的空格。如果有一些,你可以裁剪每个字符串,例如改变

str_print_bodyRecord_Array(2)

更好的方法是了解额外空间的来源,并直接在其原点解决问题。