在Visual Basic中正确格式化文本表

时间:2015-05-03 20:44:03

标签: vb.net visual-studio-2010 file-io

目前我正在尝试在Visual Basic中编写一个程序,以根据一定量的结果输出基于文本的表。

我正在尝试使用类似下面的C ++代码来正确格式化表,但无法弄清楚在Visual Basic中使用哪些代码来实现类似的效果。 (我知道coutendl是C ++特有的,但我希望VB有替代方案。

cout << endl << "Month#      Interest Amount     Monthly Amount     New Amount" << endl;

int counter = 1;
while (counter <= length * 12) {
    cout << right << setw(6) << counter; // Month Counter
}

如果没有提供过多的代码并使其变得一团糟,这就是我所拥有的。

    Dim strOutput As String
    Dim strParticipantList As String = ""

    Dim outFile As IO.StreamWriter

    strOutput = Trim(eventName & " Final Results") & vbNewLine + vbNewLine + _
                "Total Number of Participants: " + TotalParticipants & vbNewLine

    For Each storage As StopwatchStorage In _storage
        strParticipantList &= storage.ParticipantOrder.Text
    Next

我的想法是,我将遍历存储中的每个项目,并将行正确格式化为格式化为类似于C ++代码中所述的cout的字符串。

1 个答案:

答案 0 :(得分:1)

Using outFile As New IO.StreamWriter("File Path Here")
    outFile.WriteLine("{0} Final Results", eventName)
    outFile.WriteLine()
    outFile.WriteLine("Total Number of Participants: {0}", TotalParticipants)
    outFile.WriteLine()

    'Using C++ example with some made-up variable names to show how this can work
    outFile.WriteLine("Month#      Interest Amount     Monthly Amount     New Amount")
    For Each storage As StopwatchStorage In _storage
        outFile.WriteLine("{0,-6} {1,-20} {2,-18} {3,-14}", _
            storage.Month, storage.Interest, storage.MonthlyTotal, storage.NewTotal)
    Next
End Using

{x}占位符在后面的替换参数中标记索引。 {m,n}占位符使用逗号后面的部分来设置项的宽度。对于那么多字符,正数是左对齐的,负数是右对齐的。这是documentation for how it works