RichTextBox In Nice Even Columns

zunebuggy65

Active member
Joined
Oct 12, 2023
Messages
42
Programming Experience
3-5
I do not wish to use a DataGrid for what I am doing. I am instead using a RichTextBox and Courier New font. I have some text on each line that the first 10-24 characters could have varying lengths, but then on each line I have 3 integers that are zero left padded. I want to make sure that in every line these 3 integers line up. If I start the integers displaying at 29 or 30 characters from the left hand side of the RichTextBox, this is fine because the text before that will never be more than 24 characters long.

For example:

VB.NET:
10: This is some text       095 124 003

11: This another line       240 395 503

12: And yet another         105 012 000

I have tried taking each line of text before the integers and getting the length and then using
VB.NET:
n = 29 - Len(string)
and then using a For Next Loop appending n number of space before appending the integers, but the string that makes up the text before the integers is complicated and even with the fixed width font, the integers do not line up neatly.

I was just wondering if there was some string command that can be used with the RichTextBox that would automatically create nice columns of integers after the text?

I worked for years using various forms of BASIC and one of them (I do not remember which one) had a SPC or POS command but I do not see anything like that in VB.Net searching online.

Thank you.
 
You would use composite formatting. That document describes how to specify the width of a "column" and how to align text within it left or right. You just use the same format specifier for each line of text and provide the different values each time.
 
Here's a code example I wrote many years ago:
VB.NET:
Dim builder As New System.Text.StringBuilder
Dim format As String = "{0,-4}{1,-10}{2,-12:d}{3,8}"

builder.AppendFormat(format, "ID", "Name", "DOB", "Children")
builder.AppendLine()
builder.AppendFormat(format, 1, "Peter", #6/19/1969#, 2)
builder.AppendLine()
builder.AppendFormat(format, 2, "Paul", #4/23/1974#, 4)
builder.AppendLine()
builder.AppendFormat(format, 3, "Mary", #12/4/1980#, 1)
builder.AppendLine()

Me.TextBox1.Text = builder.ToString()
Here's the explanation I wrote with it:
The secret there is the format string. You can use a format string with a StringBuilder.AppendFormat, Console.WriteLine, String.Format, StreamWriter.WriteLine and other places, so your output options are many. Let's take a closer look at that format string.

It includes 4 format specifiers, so it will accept 4 values to format. You'll see that each call to AppendFormat passes the format string followed by 4 values. Funny, that. ;)

The first format specifier ({0,-4}) takes the first value ({0,-4}) and pads it out with whitespace to 4 characters ({0,-4}), left-aligning the string ({0,-4}).

The second format specifier take the second value, pads it out to 10 characters and left-aligns it.

The third format specifier ({2,-12:d}) takes the third value, pads it out to 12 characters, left-aligns it and displays it in the short date format ({2,-12:d}).

The fourth format specifier takes fourth value and pads it out to 8 characters, but this time the field width is greater than zero so it is right-aligned.
 
Back
Top