The Print Method – Part 2
Using the Comma (,) Separator
Note: The formatting option for Print discussed in this topic is only meaningful if the object you are printing to is set to a monospaced font (like Courier or Fixedsys). The output will not look right if the object is set to a proportional font (like the default MS Sans Serif, Times New Roman, etc.).
When items the expression list of the Print statement are separated by commas, VB looks at the line as a series of "zones" consisting of 14 characters each, and VB moves to the next "print zone" before printing the item following the comma. (Note: In previous versions of BASIC such as QBasic, you were limited to five zones per line; this is not true in VB).
Consider the following program segment:
Print " BAND MEMBERS"
Print "NAME", "INSTRUMENT"
Print "----", "----------"
Print "PAUL", "RHYTHM"
Print "GENE", "BASS"
Print "ACE", "LEAD"
Print "PETER", "DRUMS"
Print , , "COBO CONFERENCE CENTER"
Print , "Detroit", "Michigan", , "48226"
The Print statements above would cause the following output to be generated (the shaded area below is a guide to show you what positions the items would be printed in, the shaded portion would not actually print):
Zone 1 Zone 2 Zone 3 Zone 4 Zone 5
1 2 4 5 7
1.............5.............9.............3.............7............0
BAND MEMBERS
NAME INSTRUMENT
---- ----------
PAUL RHYTHM
GENE BASS
ACE LEAD
PETER DRUMS
COBO CONFERENCE CENTER
Detroit Michigan 48226
If a particular item consists of more than 14 characters, that item will take up as many print zones as are necessary, shifting the other items over to adjacent zones. For example, the statement
Print "ABC", "HENRY WADSWORTH LONGFELLOW", "DEF"
causes the following output to be generated:
Zone 1 Zone 2 Zone 3 Zone 4 Zone 5
1 2 4 5 7
1.............5.............9.............3.............7............0
ABC HENRY WADSWORTH LONGFELLOW DEF
Note: In all the "print zone" examples above, only string constants were used in the expression list, but of course numeric constants and string or numeric variables can certainly be used (and the "leading sign / trailing space" rule would still apply for numeric items).
If you wish, set up another "Try It" project, and place the sample Print statements in the cmdTryIt_Click event, as follows:
Private Sub cmdTryIt_Click()
Print " BAND MEMBERS"
Print "NAME", "INSTRUMENT"
Print "----", "----------"
Print "PAUL", "RHYTHM"
Print "GENE", "BASS"
Print "ACE", "LEAD"
Print "PETER", "DRUMS"
Print , , "COBO CONFERENCE CENTER"
Print , "Detroit", "Michigan", , "48226"
Print "ABC", "HENRY WADSWORTH LONGFELLOW", "DEF"
End Sub
When you run the project and click the "Try It" button, the output should look like this:
Download the VB project code for the example above here.