Solved

VB.Net Align Text columns for printing

Posted on 2014-12-29
3
507 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now