Solved

VB.Net Align Text columns for printing

Posted on 2014-12-29
3
598 Views
Last Modified: 2014-12-29
Hi Experts, I have to print multiple strings in columns so that it appears like this

Date: 25/12/2014      Time: 08H30        Name: Fred Smith              Age:27    ID Number: 123456
Date: 25/12/2014      Time: 08H30        Name: Harry Robinson      Age:7      ID Number: 456789

which I have programed as

"Date:" & "  "  & STRDAte & "        " & "Time:"  & "  " & STRTime & "       "  &   "Name:" & "  "  & STRName      etc etc  

(Actual values much more diverse but you get the idea)

Due to the differences in the length of the strings, the columns do not line up
I tried VBTab but this had no effect

Any assistance in an example of lining up the columns would be appreciated

Many Thanks
0
Comment
Question by:PNRT
3 Comments
 
LVL 3

Assisted Solution

by:Ali HND
Ali HND earned 125 total points
ID: 40521571
you can spot fix length for each string.
for example: Date String length = 16 , Time length = 12 , ...
and with string function add spaces to strings that have a less length from defined length
in operation: if Name string length is 50 and you have 'Name: Fred Smith':
calculate length of string with Len() function.
Len<50, you must add 44 spaces
For i=Len(NameStr) to 50
      NameStr= NameStr & " "
Next
So you have Constant String length and print your data like a table.
0
 
LVL 16

Assisted Solution

by:DansDadUK
DansDadUK earned 125 total points
ID: 40521589
With variable length data, you need to make each part a fixed length, so do something like the following pseudo-code (I don't know VB, so can't give you exact code):

(STRName + Fill (maxNameLength with spaces)).Substring(0, maxNameLength)

Open in new window


Also make sure that you are printing with a mono-spaced font (Courier, or Letter Gothic, or similar); most fonts (e.g. Arial, Times New, etc.) are proportionally spaced, where different characters (e.g. i, m) have different widths.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40522145
There are a PadLeft and PadRight on the String class that enables you to automatically add spaces so that a column will always display with the same number of characters:

...  &   "Name:" & "  "  & STRName .PadRight(25) & "Age:" & "  "  & STRAge .PadRight(10)

In order for this to work properly however (and this is true for most of the other methods), you need to use a non-proportional font so that each character has the same width. Consolas is one I like, because it is very similar to Arial. This is the font used by default in Visual Studio so that code aligns properly.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question