Mike Williams
12/13/2011 4:20:00 PM
"BeeJ" <nospam@spamnot.com> wrote in message
news:jc6m02$q5d$1@dont-email.me...
> This is kinda hokey...
> Debug.Print "ABC"; Tab(10); "DEF"; Tab(20); "GHI" Debug.Print
> StrTab("ABC", "Tab(10)", "DEF", "Tab(20)", "GHI")
> [StrTab function snipped]
As far as the VB Tab(n) function is concerned, a single tab "column" is the
average width of all characters in whatever font you happen to be using.
Because of this you can simulate tab columns using Space characters /only/
if all characters in that specific font are the same width (in other words,
only for a fixed width font). The reason it appears to work in your own
specific test case is that you are printing to the VB6 Debug windows which
by default uses a fixed width font (Courier New). It will not work for the
far more widely used proportionally spaced fonts, such as Arial or Times New
Roman. To see what I mean, try the following code using your own existing
StrTab function:
Private Sub Command1_Click()
Me.Font.Name = "Courier New"
Call testPrint
Me.Font.Name = "Arial"
Call testPrint
End Sub
Private Sub testPrint()
Me.Print Me.Font.Name & " using Tabs:"
Me.Print "ABC"; Tab(10); "DEF"; Tab(20); "GHI"
Me.Print "ABCDE"; Tab(10); "FGHIJ"; Tab(20); "KLM"
Me.Print Me.Font.Name & " using BeeJ Function:"
Me.Print StrTab("ABC", "Tab(10)", "DEF", "Tab(20)", "GHI")
Me.Print StrTab("ABCDE", "Tab(10)", "FGHIJ", "Tab(20)", "KLM")
Me.Print
End Sub
If you want something which you can actually embed into a string (as it
appears you do) and which works with proportionally spaced fonts then you
need to use proper Tab positions. If evenly spaced Tab positions are okay
for you then you can simply include one or more vbTab characters at the
appropriate points in your string. As far as the VB Print statement is
concerned the Tab positions are spaced 8 columns apart, where a column is
the average width of all characters in the font you are using (note that
this is not the same as the VB Print statement "Tab Print Zones" which are
spaced 14 columns apart). Each vbTab character in the string causes the
print position to move to the next fixed "8 columns apart" Tab position. For
example:
Dim s1 As String, s2 As String
Me.Font.Name = "Arial"
s1 = "ABC" & vbTab & "DEF" & vbTab & "GHI"
s2 = "ABCD" & vbTab & "EFGHIJ" & vbTab & "KLM"
Me.Print s1
Me.Print s2
If you want evenly spaced Tab positions which are spaced more (or less) than
8 columns apart then you can instead print exactly the same strings using
the GDI DrawText function, which allows you to specify the "number of
columns per Tab position".
Otherwise, if evenly spaced Tab positions are not suitable for your needs
and if you want to set irregularly spaced Tab positions (and if you are not
dumping your strings into a control which allows you to easily do that, such
as ListBox for example or a RichTextBox) then one way to do so would be to
embed control characters into your String at appropriate points and to write
a function which will print that string to a Form or PictureBox or Printer
or whatever, digging out the control characters as it does so and spacing
things out accordingly. For example, you could include a backslash character
followed by two hex digits at each point you need a Tab and your function
could use that information to calculate the desired tab positon and use a
simple CurrentX to achieve it. Your function would also need to perform the
standard "two backslashes signify a literal" thing otherwise you would not
be able to include literal backslashes in your string. There are of course
all sorts of other ways of doing these things, depending on exactly what it
is you want to do.
Mike