Results 1 to 32 of 32

Thread: Printing Textbox Text

  1. #1

    Thread Starter
    Addicted Member overhill's Avatar
    Join Date
    Mar 2000
    Location
    KS, United States
    Posts
    181

    Printing Textbox Text

    I have some text in a multiline textbox that I want to print (nothing fancy). I was using this code, which works, but it doesn't wrap the text. What do I need to do to wrap the text so that it all fits onto the page? Thanks!

    Code:
        Printer.Print Text1.Text
        Printer.Font = "Arial"
        Printer.EndDoc
    Last edited by overhill; Aug 16th, 2001 at 03:44 PM.

  2. #2
    *bump because I want to know the answer also*

  3. #3
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    Do you mean it's going off the bottom or off the right side of the page?

    Technically it should print within the margins of the page without a problem, so maybe you could give an example of the text you have in the text box.
    ~Peter


  4. #4

    Thread Starter
    Addicted Member overhill's Avatar
    Join Date
    Mar 2000
    Location
    KS, United States
    Posts
    181
    It is going off the right side of the page.

    I have text that is broken into 5-6 paragraphs. At each paragraph (vbcrlf) it drops down a line and I can see all the text until it again goes off the page (at about 155-160 characters).

    I just added some very rough code that divides things up into 150 character chunks, but it breaks up words and everything.

    I was sure that I saw some very simple code that did this somewhere, but I can't find it anywhere.

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    If you can switch to a richtext box instead, this module does a pretty good job of printing. I wish I could give credit to who wrote it, but I don't remember where I got it.

  6. #6
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by MarkT
    If you can switch to a richtext box instead, ......
    Attached is a HowTo that describes WYSIWYG for a RichTextBox control, Complete with Code for the BAS and a Project.

    -Lou

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Here you go:
    VB Code:
    1. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    2. Private Type RECT
    3.     Left As Long
    4.     Top As Long
    5.     Right As Long
    6.     Bottom As Long
    7. End Type
    8. Private Const DT_WORDBREAK = &H10
    9. Private Const DT_CALCRECT = &H400
    10.  
    11. Public Sub PrintWrapText(p_strText As String)
    12.     Dim lngOldTop As Long
    13.     Dim rec As RECT
    14.    
    15.    
    16.     lngOldTop = Printer.CurrentY
    17.     'we have to start the printer to get back the hDC
    18.     Printer.Print
    19.     With Printer
    20.         .CurrentY = lngOldTop
    21.         .ScaleMode = vbPixels
    22.         rec.Left = .CurrentX
    23.         rec.Right = .CurrentX + .Width
    24.         rec.Top = .CurrentY
    25.         'get the height of the text
    26.         DrawText .hdc, p_strText, Len(p_strText), rec, DT_WORDBREAK + DT_CALCRECT
    27.         'draw the text to the hDC of the printer
    28.         DrawText .hdc, p_strText, Len(p_strText), rec, DT_WORDBREAK
    29.         .EndDoc
    30.     End With
    31. End Sub
    Call this sub routine like
    Call PrintWrapText(Text1.Text)

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Serge,

    I'm not sure, but I don't think it works.

    I thought the Object of the question was to print the text in
    a textbox to a printer as it appears in the textbox.

    I tried your code, and It seems to be just wrapping the text to a next line when it meets the boundary of the page.

    Hmm, How'd it work for everybody else.


    -Lou

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Re: Printing Textbox Text

    I though that was the idea to wrap long text :

    Originally posted by overhill
    I have some text in a multiline textbox that I want to print (nothing fancy). I was using this code, which works, but it doesn't wrap the text. What do I need to do to wrap the text so that it all fits onto the page? Thanks!

    Code:
        Printer.Print Text1.Text
        Printer.Font = "Arial"
        Printer.EndDoc

  10. #10

    Re: Re: Printing Textbox Text

    Originally posted by Serge
    I though that was the idea to wrap long text :

    And it is.

  11. #11

    Thread Starter
    Addicted Member overhill's Avatar
    Join Date
    Mar 2000
    Location
    KS, United States
    Posts
    181
    I could get NotLKH's to work fine, but would rather not have to change to the RichTextBox control.

    Serge's code doesn't seem to change the way things print at all. Seems odd.

  12. #12
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Here is a simple way to wrap text. Uses string concatenation so may not be fastest but since u are using a printer i doubt it matters. First tests if sentences fit on line. If so, it prints them. If not it tests each word of sentence to see if it fits. Assumes left aligned whole word wrap
    Regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     lstrsentences = Split(Text1.Text, vbCrLf)
    3.     lintprinterwidth = Printer.ScaleWidth
    4.     For Counter = 0 To UBound(lstrsentences)
    5.         If Printer.TextWidth(lstrsentences(Counter)) > lintprinterwidth Then
    6.             lstrWords = Split(lstrsentences(Counter), " ")
    7.             lstrresult = ""
    8.             lintusedwidth = 0
    9.             For lintcounter = 0 To UBound(lstrWords)
    10.                 lintusedwidth = lintusedwidth + Printer.TextWidth(lstrWords(lintcounter) & " ")
    11.                 If lintusedwidth > lintprinterwidth Then
    12.                     Printer.Print lstrresult
    13.                     lintusedwidth = 0
    14.                     lstrresult = ""
    15.                 Else
    16.                     lstrresult = lstrresult & lstrWords(lintcounter) & " "
    17.                 End If
    18.             Next
    19.             Printer.Print lstrresult
    20.         Else
    21.             Printer.Print lstrsentences(Counter)
    22.         End If
    23.     Next
    24.     Printer.EndDoc
    25. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  13. #13
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    And as a sample
    VB Code:
    1. Private Sub Form_Load()
    2.     Text1.Text = "This is the first line." & vbCrLf
    3.     Text1.Text = Text1.Text & "This is the second line. A bit longer but it wraps" & vbCrLf
    4.     Text1.Text = Text1.Text & "This is the third line."
    5. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  14. #14
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Alright,here you go.

    I played around a little setting printer.CurrentX for a left margin and printer.CurrentY for line spaceing, {you should figure this part out for yourself, since I have no Idea where you want this to be printed on a page}. This gets the text of each
    line in a MultiLine TextBox, and prints it to the printer.
    Unfortunately, beachbum's code obviously keys off of Carraige Returns/LineFeeds, but as we all know, in a multiline textbox, they aren't automatically placed. It Just reflows to the next line. But this, I think, is what you want. A printOut of Every Individual Line as it lays in your textbox.

    So, Try Something Like this:
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_GETLINE = &HC4
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    
    Private Sub Command1_Click()
    Dim Num_Lines As Long
    Dim linetextlen As Integer
    Dim linetext As String
    Dim retval As Long
    Dim My_Y As Long
    Dim MyOldY As Long
    Printer.CurrentX = 1000
    Printer.CurrentY = 1000
    
    Printer.FontName = Text1.FontName
    Printer.FontSize = Text1.FontSize
    Num_Lines = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    For I = 0 To Num_Lines - 1
        MyOldY = Printer.CurrentY
        charindex = SendMessage(Text1.hwnd, EM_LINEINDEX, ByVal CLng(I), ByVal CLng(0))
        linetextlen = SendMessage(Text1.hwnd, EM_LINELENGTH, ByVal charindex, ByVal CLng(0))
        linetext = Space(IIf(linetextlen >= 2, linetextlen, 2))
        CopyMemory ByVal linetext, linetextlen, Len(linetextlen)
        retval = SendMessage(Text1.hwnd, EM_GETLINE, ByVal CLng(I), ByVal linetext)
        If linetextlen < 2 Then linetext = Left(linetext, linetextlen)
        Printer.Print linetext
        Printer.CurrentY = MyOldY + Printer.TextHeight(linetext) * 1.5
        Printer.CurrentX = 1000
    Next I
    Printer.EndDoc
    End Sub
    It gets the number of individual lines in a textbox, then it gets each individual line, and the prints each one.



    -Hope this helps
    -Lou

  15. #15
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by NotLKH

    Unfortunately, beachbum's code obviously keys off of Carraige Returns/LineFeeds, but as we all know, in a multiline textbox, they aren't automatically placed. It Just reflows to the next line. But this, I think, is what you want. Lou
    LOL... Lou. When u key data into a multi line textbox and press the enter key the vbcrlf is put into the text. If u simply wrap the sentence a vbcrlf wont be put in. But my code accounts for both. Change Printer to Picture1 remove EndDoc and try it.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  16. #16
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Stuart,

    Well, Again, I'm not sure this is what is intended. You'll see an attached image of The output, and the text inside a textbox, and as seen, the textbox text wraps, but the code produced unwrapped lines.


    -Lou


  17. #17
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    But, The more I reread his original post, The more I'm convinced that You Just Might be giving him what he wants. He doesn't say anything about matching the flow as it appears in the textbox, just that it goes off the page.


    Well, Stuart, anyways, I neglected to say,

    "Good Code!"

    Well, he's now got ample code, Mine, that match the text box flow, and yours, that keeps it on the page, and Also Serge's.
    Plus A couple of RichText Codes.

    One of these must be something he can use!


    -Lou

  18. #18
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274

    Re: Printing Textbox Text

    LOL Lou
    U just posted before i got a chance to answer. Was just gonna say that original poster had wanted to fit it on a printed page. So with this abundance of riches he is no doubt more confused than ever... another job well done i say Give us any qtn and we can make u wish u took up milk delivery as a job
    Cheers Lou
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  19. #19
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    It's not even my post, and i'm confused!

    So did we actually create code to print the long line of text in the text box? Without it going off the printed page?
    ~Peter


  20. #20
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301
    This is the way I used to split long lines using plain text:
    I used the Printer.TextLength property to check if each line of text is longer than 180 mm, then substract the last word and print the line:


    Code:
    Dim MyPhrase  As String
    Dim Phrase(100)  As String
    Dim MyText As String
    Dim lin, pal As String
    Dim m, p, q, k As Integer
    
    Printer.Scale (0, 0)-(200, 280)
    'This scale is in millimeters and works well for an A4 sheet
    
    MyText = Text1.Text
        p = InStr(MyText, vbCrLf)
        q = 1
        k = 1
    While p
    
    'Here I split the text into Phrases, by means of vbCrLf:
        If p Then
            Phrase(k) = Left(MyText, p - 1)
            MyText = Right(MyText, Len(MyText) - p - 1)
        End If
        p = InStr(MyText, vbCrLf)
        k = k + 1
    Wend
    'This is the LAST Phrase -whatever is left after splitting:
    Phrase(k) = MyText
    
    'Now, for each individual Phrase, I form LINES (lin) of a given length:
    'In this case I selected 180 mm
    'Each lin is formed wy several WORDS (pal) until it reaches the length I choosed
    For m = 1 To k
    p = InStr(Phrase(m), " ")
      While p
        pal = Left(Phrase(m), p)
        Phrase(m) = Right(Phrase(m), Len(Phrase(m)) - p)
            If Printer.TextWidth(lin & pal) > 180 Then
                Printer.CurrentX = 20
                Printer.Print lin
                lin = ""
            End If
        lin = lin & pal
        p = InStr(Phrase(m), " ")
      Wend
    Printer.CurrentX = 20
    Printer.Print lin & Phrase(m)
    lin = ""
    'I clear lin for the next cycle
    Next m
    Printer.EndDoc
    Hope it helps!
    Combat poverty: kill a poor!!

  21. #21

  22. #22
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    I gotta start keeping a list of Raab awards for digging up old threads. Going on my poor memory I think that Juan is the 13th daily winner. Congrats!!! Balloons, streamers etc
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  23. #23

  24. #24
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    All of my posts are pure class Lou.... (hope that puts him off the trail!! )

    Mine are like this
    50%: Hey u friggin goober, what the hell u talkin about?
    24%: Can anyone find my keys?
    15%: My head hurts, no more beer
    27%: I am not so good at maths
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  25. #25
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Just a thought Lou you could always pull up that compiler / decompiler / encrypter / decrypter thread in a few more months if u need somewhere to spit your coffee
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  26. #26
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Errrrr, whaaa?

    Is that one of Mine or one of Yours?

    Mind you, I had just pulled up your threads before you so rudely BEEBOOO'ed {sound of Incoming mail} me back to here.


  27. #27
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  28. #28

  29. #29
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    It's threads like this one that make me love VB World so much!
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  30. #30
    Lively Member
    Join Date
    Dec 2002
    Posts
    107
    Use this:

    <vbcode>
    Private Sub TBPrintWrap(ByVal Text As String, ByVal LtMar As Long, ByVal RtMar As Long)
    Dim I As Integer
    Dim j As Integer
    Dim currWord As String


    Printer.CurrentX = LtMar
    I = 1

    Do Until I > Len(Text)
    currWord = ""
    Do Until I > Len(Text) Or Mid$(Text, I, 1) <= " "

    currWord = currWord & Mid$(Text, I, 1)

    I = I + 1
    Loop

    If (Printer.CurrentX + Printer.TextWidth(currWord)) > (Printer.ScaleWidth - RtMar + Printer.ScaleLeft) Then

    Printer.Print
    Printer.CurrentX = LtMar

    End If

    Printer.Print currWord;
    Do Until I > Len(Text) Or Mid$(Text, I, 1) > " "

    Select Case Mid$(Text, I, 1)

    Case " "
    Printer.Print " ";

    Case Chr$(10) 'LF

    Printer.Print

    Printer.CurrentX = LtMar

    Case Chr$(9) 'Tab

    j = (Printer.CurrentX) / Printer.TextWidth("0")

    j = j + (10 - (j Mod 10))

    Printer.CurrentX = (j * Printer.TextWidth("0"))

    Case Else

    End Select

    I = I + 1

    Loop

    Loop

    End Sub

    '---------------

    next, within the print event (printer click button or menu print option), include the code:

    'other printer stuff

    Printer.CurrentY = 8400 ' 8400 twips down from top of page..
    TBPrintWrap txtProcess.Text, 1100, 1100 ' X location, for both margins, assumed equal

    'other stuff

    Printer.EndDoc ' start printing
    </vbcode>

  31. #31
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    thread reviv0rz :P
    Our Father, who 0wnz heaven, j00 r0ck!
    May all 0ur base someday be belong to you!
    May j00 0wn earth just like j00 0wn heaven.
    Give us this day our warez, mp3z, and pr0n through a phat pipe.
    And cut us some slack when we act like n00b lamerz, just as we teach n00bz when they act lame on us.
    Please don't give us root access on some poor d00d'z box when we're too pissed off to think about what's right and wrong, and if you could keep the fbi off our backs, we'd appreciate it.
    For j00 0wn r00t on all our b0x3s 4ever and ever, 4m3n.

  32. #32
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    The Netherlands
    Posts
    425
    Originally posted by mikeycorn
    It's threads like this one that make me love VB World so much!

    I know exactly what you mean! Same here !
    "Experience is something you don't get until just after you need it."

Posting Permissions

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



Click Here to Expand Forum to Full Width