Aligning Text

I have a CSV file which has 2 columns. Column 1 has anywhere between 5 & 30 characters & column 2 can have between 15 & 50 characters

I want to format this data:

From:
-----

Column one's data     Columns one's value here
Column two's data here     Columns two's value here
Column three's data     Columns three's value here

To:
----

Column one's data              Columns one's value here
Column two's data here       Columns two's value here
Column three's data            Columns three's value here

Aligned in a textbox or label as shown above.

How do I do this because i want this to be aligned in an ASPX page using a label

The answer will be the same for a label on a Windows form though. For this application I am using VB.NET 2003

Any help would be appreciated.

Thank you in advance


Answer this question

Aligning Text

  • ddaaddoo

    Hello there
    As promised, some code:


    Sub FillLabel()
            Dim intMaxLen, intFor As Integer
            Dim strVal(2), strDat(2), strtest As String

            strVal(0) = "Column one Value here"
            strVal(1) = "Column two Value here"
            strVal(2) = "Column three Value here"
            strDat(0) = "Column one Data"
            strDat(1) = "Column two Data here"
            strDat(2) = "Column three Data"

            'Enumerate max. Length
            intMaxLen = 0
            For intFor = 0 To strDat.GetLength(0) - 1 'We need to subtract 1 since we use a Zero-Based index in Arrays
                intMaxLen = Math.Max(intMaxLen, strDat(intFor).Length)
            Next

            'We now have to add spaces to the other values, so they have the same Length
            For intFor = 0 To strDat.GetLength(0) - 1
                While strDat(intFor).Length < intMaxLen
                    strDat(intFor) &= " "
                End While
            Next

            'Now all strVals should be the same length (At least i hope so!)
            'Let's now fill the Label:'
            lblText.Text = ""
            For intFor = 0 To strVal.GetLength(0) - 1
                lblText.Text &= strDat(intFor) & " " & strVal(intFor) & vbCrLf
            Next

            'In theory we should be done now!

        End Sub


     


    WARNING: This code will only work if the font that's used is COURIER NEW or something similar! (Fixed width font!)

  • Coenie

    Hello there!

    I guess you could enumerate the length of the longest value and let VB fill the other values with spaces until they reach the length of the longest value.

    I will provide a code sample a bit later.

  • Aligning Text