Using IF statements to find string length in array for alignment (Visual Basic)
Posted
by
Brodoin
on Stack Overflow
See other posts from Stack Overflow
or by Brodoin
Published on 2012-10-13T03:41:55Z
Indexed on
2012/10/13
15:38 UTC
Read the original article
Hit count: 267
My question is just as it says in the title. How would one use IF statements to find the string-length of content in an array, and then make it so that they show up in a Rich Text Box with the left sides aligned?
Noting that one value in my array is a Decimal.
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
Output
It shows up with vbTabs in my output, but still, it looks similar in that they are not aligned. The first two do, but after that they are not, and I am totally lost.
P0001 Coffee - Colombian Supreme 24/Case: Pre-Ground 1.75 Oz Bags $16.50
P0002 Coffee - Hazelnut 24/Case: Pre-Ground 1.75 Oz Bags $24.00
P0003 Coffee - Mild Blend 24/Case: Pre-Ground 1.75 Oz Bags $20.50
P0004 Coffee - Assorted Flavors 18/Case. Pre-Ground 1.75 Oz Bags $23.50
P0005 Coffee - Decaf 24/Case: Pre-Ground 1.75 Oz Bags $20.50
© Stack Overflow or respective owner