Solved

VB.Net Align Text columns for printing

Posted on 2014-12-29
3
645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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