VB Tab Stops – replacement

I had a quick look at how to set a tab stop in Visual Basic and although it can be done, I think it might be a little beyond the scope of the course, so I looked for a better way of doing it. Here is what I found

ToString.PadRight

The above function allows us to create strings with padding (spaces) to a predetermined length.  The program below displays 3 columns of numbers.

Dim Counter As Integer
For Counter = 1 To 20
ListBox1.Items.Add(Counter.ToString.PadRight(10) & (Counter * 10).ToString.PadRight(10) & Counter * 100)
Next
Notice the () around the Counter*10 without these, the program displays the last 2 columns concatenated without the padding.
String padding is NOT the same as using a TAB! Tabs do not change the string they move, this method adds spaces to the string and then outputs one continuous string.  The output string in this case is about 24 characters where using  tabs would only use about 10.
In the image below I have shown this by changing the padding character to a full stop. However, I think it is good enough for higher so I am quite happy for you to use it.
WARNING – using this shows another difference between a tab and a bunch of spaces. A tab is a predefined distance, where a character can vary in size due to proportional fonts or kerning.  When you use this method change the font in your list box to a monspaced font like courier. That way each character takes up the same number of pixels and your table will be formatted correctly.

2 thoughts on “VB Tab Stops – replacement”

    1. Use a for loop to traverse the array. Inside the for loop use a listbox add items to display the current array element for each array.
      IE
      For counter = 1 to 5
      ListBox1.Items.Add(NAME(Counter) & vbTab & age(Counter))
      Next
      This allows for the contents of both arrays to be displayed.

Comments are closed.