VB .NET - 如何用点设置vbTab?

时间:2016-07-28 03:03:32

标签: vb.net

背景:
使用文本(完成)创建日志文件,但未设置vbTab成功

<小时/> 格式:
[DateTime] [UserName] [ClassName] [vbTab] [LogIndexMessage]
例如:

[01 Jan,2016 24:59:59 PM][LocalHost\User][Class1]              This is Message1
[01 Jan,2016 24:59:59 PM][LocalHost\User][Class2NameWasLongerThanClass1Name]              This is Message2

<小时/> 如果我想设置一个带有标签位置的固定点,使其像:

[01 Jan,2016 24:59:59 PM][LocalHost\User][Class1]                             This is Message1
[01 Jan,2016 24:59:59 PM][LocalHost\User][Class2NameWasLongerThanClass1Name]  This is Message2

<小时/> 我怎样才能将标签设置到某个固定的位置,就像我之前说过的那样? 大多数人告诉我,还有另一种设置空间方面的方法:

Log_String = String.Format("{0,-7}{1,-2}{2,-2}{3,-50}", _Date & _User & _Class & _Message)

但是对于某些字符大小不一样,所以我知道将Tab设置到某个位置的任何方式?

最诚挚的问候,
Koi Tsang

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    Dim strTag = vbTab
    Dim iLen = 100
    Dim strTime1 = "[01 Jan,2016 24:59:59 PM][LocalHost\User][Class1]"
    Dim strMg1 = "This is Message1"
    Dim strTime2 = "[01 Jan,2016 24:59:59 PM][LocalHost\User][Class2NameWasLongerThanClass1Name]"
    Dim strMg2 = "This is Message2"
    Dim iChar1 = strTime1.Length + strMg1.Length
    Dim strRow1 = strTime1 & strTag.PadLeft(iLen - iChar1) + strMg1

    Dim iChar2 = strTime2.Length + strMg2.Length
    Dim strRow2 = strTime2 & strTag.PadLeft(iLen - iChar2) + strMg2