我的問題與標題中所述的一樣。如何使用IF語句來查找數組中的內容的字符串長度,然後使其顯示在Rich Text Box中,左側對齊?使用IF語句查找對齊數組中的字符串長度(Visual Basic)
注意到我的數組中有一個值是十進制數。
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 /箱體:預磨1.75盎司 手袋$ 16.50
P0002咖啡 - 榛子24 /箱體預磨1.75盎司袋$ 24.00
P0003咖啡 - 輕度混合24 /箱體:預磨1.75盎司手袋$ 20.50
P0004咖啡 - 各種口味18 /盒。預磨1.75盎司 手袋$ 23.50
P0005咖啡 - 無咖啡因24 /箱體預磨1.75盎司手袋$ 20.50
它顯示了在我的輸出vbTabs,不過,它看起來類似他們沒有對齊。前兩個是,但之後他們沒有,我完全失去了。
您需要提供一些代碼。很高興看到你迄今爲止做了什麼來解決這個問題,但至少對你來說顯示輸入(使用聲明性代碼)和你想要的輸出是很好的。我們中的很多人在發佈之前使用LINQPad等工具來測試我們的答案。如果你能給我們輸入代碼,它使我們的工作變得更容易。 – Enigmativity
添加了我的代碼。我需要做到這一點,所以左側對齊在每列中對齊。我的教授說這可以使用IF語句來對抗這個長度,然後我猜想可以添加vbTabs。 – Brodoin