我的问题就像标题中所说的那样。如何使用 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 咖啡 - 哥伦比亚 Supreme 24/箱:预磨粉 1.75 盎司袋装 16.50 美元
P0002 咖啡 - 榛子 24/箱:预磨粉 1.75 盎司袋装 $24.00
P0003 咖啡 - 温和混合 24/箱:预磨粉 1.75 盎司袋装 20.50 美元
P0004 咖啡 - 什锦口味 18/箱。Pre-Ground 1.75 盎司袋装 $23.50
P0005 咖啡 - 无咖啡因咖啡 24/箱:预磨粉 1.75 盎司袋装 $20.50
它在我的输出中显示为 vbTabs,但看起来仍然相似,因为它们没有对齐。前两个有,但之后没有,我完全迷路了。