locked
Aligning data to be printed using tab RRS feed

  • Question

  • Previously, in VB6, when formatting a data string to be printed, we could use Tab(xx) to ensure a value was printed in a specific column (ie Tab(15) would begin printing the value in column 15).  Is there an equivalent in VB.NET that can be used with drawstring method?


    Tuesday, December 26, 2017 8:35 PM

All replies

  • PS I just realized, duh, If you want to use vbtab(n) then you need to import Microsoft.VisualBasic namespace. Then you can use those vb constants.

    ==========

    That would be ascii chr(9).

    Here is a quick example. There are other methods too.

    Imports System.Drawing.Printing
    
    Public Class Form2
        Private WithEvents pd As New PrintDocument
        Private Lines As New List(Of String)
    
        Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i As Integer = 0 To 10
                Lines.Add("line " & i.ToString & Chr(9) & "A" & Chr(9) & "A")
                Lines.Add("line " & i.ToString & Chr(9) & "bbbA" & Chr(9) & "bbbA")
            Next
            Dim preview As New PrintPreviewDialog
            preview.Document = pd
            preview.ShowDialog()
        End Sub
    
        Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            Static I As Integer
            Dim y As Single = e.MarginBounds.Top
            Dim fontH As Single = CSng(e.Graphics.MeasureString("Xg", Font).Height)
    
            Do While I < Lines.Count - 1
                e.Graphics.DrawString(Lines(I), New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, 200, y)
    
                y += fontH
    
                If y > e.MarginBounds.Bottom Then
                    'if more text beyond bottom without image
                    e.HasMorePages = True
                    Exit Do
                End If
    
                I += 1
            Loop
        End Sub
    
    End Class






    Tuesday, December 26, 2017 9:08 PM
  •  I see that the old VB6 leftover vbTab has an index parameter but,  i have not a clue how it is used or if that is what you are looking for.  The msdn documents on it don't even mention it.  I normally set up columns using the X position,  never really use tabs.  8)

     vbTab(index)

     

     Huh,  i did not refresh my page and i see Tom already added that to his post.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, December 26, 2017 10:30 PM
    Tuesday, December 26, 2017 10:23 PM
  • Tom,

     Is there a reason you are using the form's font to get the font height and then using a different font to print the string?  By the way,  the font height is already a Single,  you shouldn't have to convert it to a Single.   Just nitpicking.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, December 26, 2017 10:33 PM
    Tuesday, December 26, 2017 10:31 PM
  • Tom,

     Is there a reason you are using the form's font to get the font height and then using a different font to print the string?  By the way,  the font height is already a Single,  you shouldn't have to convert it to a Single.   Just nitpicking.  8)


    If you say it can`t be done then i`ll try it


    Goes back to the typewriter. Vbtab(3) is 3 tabs. Then, on a typewriter, you move the little things on top for the tab stops. I forget exactly.

    In Ms Word I think there are little handles to drag.

    I am not sure how you set them in vb. But tab 1 might be 100 and 2 150and 3 275, so that's the thing they are not necessarily equal.

    The example was actually ops code from some other I grabbed Professor. That is my excuse.  :)

    Maybe Stephan can show us how they work now.

    PS But yes you are right on those things you point out. I also noticed the static I needs to be reset somehow. Its just to show the tabs with chr(9). So you could, repeat chr(9) 3 times in the string and if the tab space is 10 then you go 30 or 3 tab stops.

    And so on the typewriter the stops are mechanical and the carriage slides to them and hits a pin or something you can set and stops. Maybe you know already.

    Oh yes, see the tab stops are not affected by the font. Its not font character widths exactly. Its spaces I guess, maybe the font height would change the space, but not like you get with a font in a string. So you get the starting character lining up in columns at the tab stops.

    Tuesday, December 26, 2017 10:42 PM
  • Previously, in VB6, when formatting a data string to be printed, we could use vbTab(xx) to ensure a value was printed in a specific column (ie vbTab(15) would begin printing the value in column 15).  Is there an equivalent in VB.NET that can be used with drawstring method?

    You can also use String.Format for alignment:

    https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx#Format_Syntax

    For example:

    Public Class Form1
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            Dim years() As Integer = { 2013, 2014, 2015 }
            Dim population() As Integer = {1025632, 1105967, 1148203}
    
            Dim sb As New System.Text.StringBuilder
    
            sb.AppendLine(String.Format("{0,6} {1,15}", "Year", "Population"))
    
            sb.AppendLine()
            sb.AppendLine()
    
            For index As Integer = 0 To years.Length - 1
                sb.AppendLine(String.Format("{0,6} {1,15:N0}", years(index), population(index)))
            Next
    
            MessageBox.Show(sb.ToString)
           
            Stop
    
        End Sub
    End Class

    That's from the example shown in that documentation but I changed it some to use a StringBuilder. Hopefully you get the idea though.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, December 26, 2017 11:17 PM
  •  Oh... i see said the blind man.   8)

     I guess if all else fails,  you could use the String constructor to make several tabs in a row...

    New String(Chr(9), 3) 'makes a string of 3 tabs

     I tried using the vbTab(index) and it just kept giving an index out of range exception no matter what i set it to.  I guess i just ain't not smart enuf to yous the old VB6 methods.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, December 26, 2017 11:32 PM
    Tuesday, December 26, 2017 11:29 PM
  •  Oh... i see said the blind man.   8)

     I guess if all else fails,  you could use the String constructor to make several tabs in a row...

    New String(Chr(9), 3) 'makes a string of 3 tabs

     I tried using the vbTab(index) and it just kept giving an index out of range exception no matter what i set it to.  I guess i just ain't not smart enuf to yous the old VB6 methods.  8)


    If you say it can`t be done then i`ll try it

    Yes that is correct. I suspect if you drill down the vbtab it is chr(9) , like vblf,is chr(13) I think. And then vbtab(n) is a function that returns a string of chr(9).

    But as I say those spacings are somehow font independent or something.


    PS You must have to set tab.space = 20 first? But 20 what?
    Tuesday, December 26, 2017 11:35 PM
  • Tommy,

    To do what you're talking about, the String class has its own:

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.space(v=vs.110).aspx

    It's just "Space(value)"


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, December 26, 2017 11:40 PM
  • Tommy,

    To do what you're talking about, the String class has its own:

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.space(v=vs.110).aspx

    It's just "Space(value)"


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Frank,

    Yes, that's right.

    However, I am not sure it is the same thing. Since I am not sure I should probably stop there.

    However, I don't think printing 10 spaces between words from string builder with ms sans serief will be the same as using a tab stop set on 10 '100/ths of something. Or maybe it is?

    :)

    Tuesday, December 26, 2017 11:52 PM

  • However, I don't think printing 10 spaces between words from string builder with ms sans serief will be the same as using a tab stop set on 10 '100/ths of something. Or maybe it is?

    :)

    I'm sure you're right. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, December 26, 2017 11:54 PM

  •  I tried using the vbTab(index) and it just kept giving an index out of range exception no matter what i set it to.  I guess i just ain't not smart enuf to yous the old VB6 methods.  8)


    If you say it can`t be done then i`ll try it

    Razerz,

    Well, now that you mention it, I am not sure there is a vbtab(n), just a vbtab. I did not look at the ref I gave close enough? Or not. I dont want to look for it.

    So its string(10, vbtab) or something like you showed.


    PS string(vbtab, 3) is same as vbtab(3) a hypothetical function.
    Wednesday, December 27, 2017 12:32 AM
  •   After looking around i guess that vbTab has an (index) parameter simply because it is a String type which has an (index) parameter to get a single Char from a String at a specific index.  In the .Net source code it is simply just a Constant declared as this...

    vbTab = "\t"

     So,  the index parameter is useless because vbTab only has 1 Char in the string,  a tab character.  It wont throw an exception if you do vbTab(0) but,  it would make no sense to do so.  It is basically the same thing as doing this in .Net..

            Dim s As String = "A"
            Dim ss As String = s(0) 'this would be the first char in s which is "A". Any index higher than 0 will throw index out of range exception
    

     So,  it still leaves you with adding several vbTab's together,  or using the String constructor as i showed,  or using the String.Format as Frank has shown.  I suppose you could even use the String.Join method.  Other than that,  you are left with just setting up the X location to print each column which is my personal preference.  It all depends on where you get the data strings from and what format they are in that would determine the easiest method to use.

     

    If you say it can`t be done then i`ll try it

    Wednesday, December 27, 2017 2:03 AM