Solved

VB.Net Align Text columns for printing

Posted on 2014-12-29
3
700 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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