[RESOLVED] Printing Tab Character
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Printing Tab Character

  1. #1
    Join Date
    Jun 2009
    Posts
    20

    [RESOLVED] Printing Tab Character

    I can't find a suitable answer to this issue anywhere, including on this forum. SOmeone raised the dame question (Nov '10) but no answer so don't know if solution exists. Plenty of replies to 'replace the tab with spaces' but that does not align the text correctly. It can't be that hard, can it?

    I'm printing a textbox which prints correctly except the tab chars are missing. So instead of:
    123 456

    I get:
    123456

    As mentioned replacing tabs with spaces is not suitable. What trick am i missing?
    (VB.Net 2010)

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    Re: Printing Tab Character

    When you say printing a textbox do you mean sending the data from a textbox to the printer or are you talking about something else?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2009
    Posts
    20

    Re: Printing Tab Character

    Quote Originally Posted by DataMiser View Post
    When you say printing a textbox do you mean sending the data from a textbox to the printer or are you talking about something else?
    Yes, sorry I can se the ambiguity in my question. I'm printing the textbox contents:

    Private Sub prtDoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles prtDoc.BeginPrint
    StringToPrint = Me.txtBody.Text
    End Sub

    Private Sub prtDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prtDoc.PrintPage
    Dim numChars%, numLines%
    Dim stringForPage$
    Dim strFormat As New StringFormat()
    Dim PrintFont As Font = Me.txtBody.Font
    'Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
    'Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
    Dim rectDraw As New RectangleF(50, 100, 727, 969)
    Dim sizeMeasure As New SizeF(727, 969 - PrintFont.GetHeight(e.Graphics))
    '--------------------------
    strFormat.Trimming = StringTrimming.Word
    e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
    stringForPage = StringToPrint.Substring(0, numChars)
    e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
    If numChars < StringToPrint.Length Then
    StringToPrint = StringToPrint.Substring(numChars)
    e.HasMorePages = True
    Else
    e.HasMorePages = False
    End If
    End Sub

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: Printing Tab Character

    You need to use the StringFormat.SetTabStops Method:

    http://msdn.microsoft.com/en-us/libr...ttabstops.aspx

  5. #5
    Join Date
    Jun 2009
    Posts
    20

    Re: Printing Tab Character

    Quote Originally Posted by Peter_B View Post
    You need to use the StringFormat.SetTabStops Method:

    http://msdn.microsoft.com/en-us/libr...ttabstops.aspx
    Thanks so much for that, Peter, exactly what I needed. Waited for weeks on another forum with over 50 reads and zero replies! Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center