使用IF语句在数组中查找字符串长度以进行对齐(Visual Basic)

时间:2012-10-13 03:41:56

标签: arrays vb.net if-statement alignment string-length

我的问题正如标题中所述。如何使用IF语句在数组中查找字符串长度的内容,然后使它们显示在左侧对齐的富文本框中?

注意到我的数组中的一个值是十进制。

Imports System.IO
Imports System.Convert

Public Class frmAll
    'Declare Streamreader
    Private objReader As StreamReader

    'Declare arrays to hold the information
    Private strNumber(24) As String
    Private strName(24) As String
    Private strSize(24) As String
    Private decCost(24) As Integer

    Private Sub frmAll_Load(ByVal sender As System.Object,
                            ByVal e As System.EventArgs) Handles MyBase.Load
        'Set objReader
        objReader = New StreamReader("products.csv")
        'Call the FillArray sub to fill the array
        Call FillArray()
    End Sub

    Private Sub FillArray()
        'Declare variables and arrays
        Dim decCost(24, 1) As Decimal
        Dim strFields() As String
        Dim strRec As String
        Dim intCount As Integer = 0
        Dim chrdelim As Char = ToChar(",")
        'Set strRec to read the lines
        strRec = objReader.ReadLine

        'Do while loop to fill array.
        Do While strRec <> Nothing
            strFields = strRec.Split(chrdelim)
            strNumber(intCount) = strFields(0)
            strName(intCount) = strFields(1)
            strSize(intCount) = strFields(2)
            decCost(intCount, 0) = ToDecimal(strFields(3))
            decCost(intCount, 1) = ToDecimal(strFields(4))
            'Set strRec to read the lines again
            strRec = objReader.ReadLine
            'increment the index
            intCount += 1
        Loop
        'Call the Calculate sub for calculation
        Call Calculate(decCost)
    End Sub

    Private Sub Calculate(ByVal numIn(,) As Decimal)
        'Define arrays to hold total cost
        Dim decRowTotal(24) As Decimal

        'Define variables to hold the counters for rows and columns
        Dim intR As Integer
        Dim intC As Integer

        'Calcualte total cost
        For intC = 0 To 1
            For intR = 0 To 24
                decRowTotal(intR) += numIn(intR, intC) * 1
            Next
        Next
        'Call the Output sub to configure the output.
        Call Output(numIn, decRowTotal)

    End Sub

    Private Sub Output(ByVal NumIn(,) As Decimal, _
                       ByVal RowTotalIn() As Decimal)
        'Variables
        Dim strOut As String

        Dim intR As Integer = 0
        Dim intC As Integer = 0
        'Set header for output.
        strOut = "ID" & vbTab & "Item" & vbTab & vbTab & vbTab & "Size" & _
            vbTab & vbTab & vbTab & vbTab & "Total Price" & _
            vbCrLf & "---------- ... -------------------------" & vbCrLf

        'For loop to add each line to strOut, setting
        'the RowTotalIn to currency.
        For intC = 0 To 24
            strOut &= strNumber(intC) & vbTab
            strOut &= strName(intC) & vbTab
            strOut &= strSize(intC) & vbTab
            strOut &= RowTotalIn(intC).ToString("c") & vbCrLf
        Next
        'Add strOut to rbtAll
        rtbAll.Text = strOut
    End Sub
End Class

输出:

  

P0001咖啡 - 哥伦比亚至尊24 /案例:Pre-Ground 1.75盎司   袋装$ 16.50

     

P0002咖啡 - 榛子24 /案例:预磨1.75盎司袋$ 24.00

     

P0003咖啡 - 温和混合24 /盒:预磨1.75盎司袋$ 20.50

     

P0004咖啡 - 各种口味18 /盒。预磨1.75盎司   袋装$ 23.50

     

P0005咖啡 - Decaf 24 /盒:预磨1.75盎司袋$ 20.50

它在我的输出中显示了vbTabs,但是,它看起来很相似,因为它们没有对齐。前两个做了,但之后他们没有,我完全迷失了。

2 个答案:

答案 0 :(得分:1)

好吧,除非你使用固定宽度的字体,否则字符串长度并不真正相关。如果你是,你想要做的是使用String.PadLeft,而不是字符串的长度。

答案 1 :(得分:1)

为了测量文本的长度(以像素为单位),您可以编写

pixels = System.Windows.Forms.TextRenderer.MeasureText(text, font)

MeasureText方法有许多重载,其参数会影响测量。问题是,您无法真正了解RichTextBox如何呈现和计算文本长度。但是,与使用比例字体计算文本中的字符数相比,此方法将产生更精确的结果。看到这个例子,两行都是十个字符长:

  

IIIIIIIIII
  mmmmmmmmmm


<强>更新

使用标签格式化文字的说明。带有两列的示例,其中第二列从第5个选项卡位置开始。

Column #1                               Column #2

------>|------>|------>|------>|------>|Some text
Short->|------>|------>|------>|------>|Some more text
A bit longer-->|------>|------>|------>|Even more text
An even longer text--->|------>|------>|The last one

如您所见,根据第一列中文本的长度,您必须在第二列之前添加不同数量的选项卡。在第一行,第一列是空的,在第二行,有一个比标签宽度短的文本。在这两种情况下,您都需要插入5个标签。在第3行和第4行,文本较长,您只需要分别插入4和3个标签。

这可以让您了解如何计算正确数量的标签。