Search code examples
.netvb.netdynamic-dataspace

VB.Net equal space output of string


Thought of really cool utility for my engineering job, but alas, having issues with something I thought would be simple. My goal is to easily apply unequal spacing across for uniform fields... I have researched padding because that sounds like the solution but I ended up getting the same output as the following.

RichTextBox2.Text = "HOSTNAME       IP       LOCAL       REMOTE    PLATFORM "
                     & vbNewLine

Dim largest = 0
For Each host In hostnames
        If host.ToString.Length > largest Then
            largest = host.ToString.Length
        End If
Next

For count As Integer = 0 To hostnames.Count - 1
        Dim space = largest - hostnames(count).ToString.Length
        RichTextBox2.AppendText(hostnames(count).ToString)

   For x = 0 To space -1
            RichTextBox2.AppendText(" ")
   Next 
Next

HERE IS AN EXAMPLE OF WHAT IM GETTING

SW_1123_TEST              1123.45.6.78
SW_2        123.4
SW_3+4_T         23.123.41.21
S_1        123.4.5.67

Solution

  • As said via comments, you cannot rely on this kind of approaches (adding blank spaces) without bringing the size of the given font into account. Additionally, the RichTextBox has a SelectionTabs property pretty helpful on these situations. Sample code:

    Dim inputs1 As New List(Of String)
    Dim inputs2 As New List(Of String)
    Dim inputs3 As New List(Of String)
    
    inputs1.Add("1100")
    inputs1.Add("120")
    inputs1.Add("130000")
    
    inputs2.Add("210")
    inputs2.Add("22000")
    inputs2.Add("2300")
    
    inputs3.Add("31000")
    inputs3.Add("3200")
    inputs3.Add("3300")
    
    Dim largest(3) As Integer
    
    Dim curInputs As List(Of String) = inputs1
    Dim count As Integer = -1
    Do
        count = count + 1
        If (count = 1) Then curInputs = inputs2
        If (count = 2) Then curInputs = inputs3
        For Each item In curInputs
            Dim curLength As Integer = TextRenderer.MeasureText(item, RichTextBox2.Font).Width
            If curLength > largest(count) Then
                largest(count) = curLength
            End If
        Next
    Loop While (count < 2)
    
    RichTextBox2.SelectionTabs = New Integer() {largest(0), largest(1), largest(2)} 
    count = -1
    Do
        count = count + 1
    
        RichTextBox2.Text = RichTextBox2.Text & inputs1(count) & vbTab & inputs2(count) & vbTab & inputs3(count) & Environment.NewLine
    Loop While (count < 2)
    

    I rely on different lists (one per "column") to show the ideas clearly. Firstly, you have to determine the longest length per column, by relying on the length of the text on account of the given font (via TextRenderer.MeasureText). After that you have to re-dimension SelectionTabs to account for the number of columns you want and set the lengths from the aforementioned maximum values. Finally, write the values by specifying where the "separation between columns" should be placed (vbTab).

    NOTE: the default RichTextBox configuration seems to account for the tabs automatically without any indication. Just by writing the last loop (including only the writing part) the default RichTextBox (VS 2010) shows the expected behaviour (3 "columns").