|
-
Aug 16th, 2001, 03:22 PM
#1
Thread Starter
Addicted Member
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.
-
Aug 16th, 2001, 03:36 PM
#2
Member
*bump because I want to know the answer also*
-
Aug 16th, 2001, 03:58 PM
#3
Frenzied Member
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

-
Aug 16th, 2001, 04:10 PM
#4
Thread Starter
Addicted Member
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.
-
Aug 16th, 2001, 04:43 PM
#5
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.
-
Aug 16th, 2001, 04:55 PM
#6
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
-
Aug 16th, 2001, 05:00 PM
#7
Here you go:
VB Code:
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
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const DT_WORDBREAK = &H10
Private Const DT_CALCRECT = &H400
Public Sub PrintWrapText(p_strText As String)
Dim lngOldTop As Long
Dim rec As RECT
lngOldTop = Printer.CurrentY
'we have to start the printer to get back the hDC
Printer.Print
With Printer
.CurrentY = lngOldTop
.ScaleMode = vbPixels
rec.Left = .CurrentX
rec.Right = .CurrentX + .Width
rec.Top = .CurrentY
'get the height of the text
DrawText .hdc, p_strText, Len(p_strText), rec, DT_WORDBREAK + DT_CALCRECT
'draw the text to the hDC of the printer
DrawText .hdc, p_strText, Len(p_strText), rec, DT_WORDBREAK
.EndDoc
End With
End Sub
Call this sub routine like
Call PrintWrapText(Text1.Text)
-
Aug 16th, 2001, 05:15 PM
#8
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
-
Aug 16th, 2001, 05:21 PM
#9
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
-
Aug 16th, 2001, 05:22 PM
#10
Member
Re: Re: Printing Textbox Text
Originally posted by Serge
I though that was the idea to wrap long text :
And it is.
-
Aug 16th, 2001, 05:22 PM
#11
Thread Starter
Addicted Member
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.
-
Aug 16th, 2001, 09:16 PM
#12
PowerPoster
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:
Private Sub Command1_Click()
lstrsentences = Split(Text1.Text, vbCrLf)
lintprinterwidth = Printer.ScaleWidth
For Counter = 0 To UBound(lstrsentences)
If Printer.TextWidth(lstrsentences(Counter)) > lintprinterwidth Then
lstrWords = Split(lstrsentences(Counter), " ")
lstrresult = ""
lintusedwidth = 0
For lintcounter = 0 To UBound(lstrWords)
lintusedwidth = lintusedwidth + Printer.TextWidth(lstrWords(lintcounter) & " ")
If lintusedwidth > lintprinterwidth Then
Printer.Print lstrresult
lintusedwidth = 0
lstrresult = ""
Else
lstrresult = lstrresult & lstrWords(lintcounter) & " "
End If
Next
Printer.Print lstrresult
Else
Printer.Print lstrsentences(Counter)
End If
Next
Printer.EndDoc
End Sub
-
Aug 16th, 2001, 09:17 PM
#13
PowerPoster
And as a sample
VB Code:
Private Sub Form_Load()
Text1.Text = "This is the first line." & vbCrLf
Text1.Text = Text1.Text & "This is the second line. A bit longer but it wraps" & vbCrLf
Text1.Text = Text1.Text & "This is the third line."
End Sub
-
Aug 16th, 2001, 11:40 PM
#14
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
Last edited by NotLKH; Aug 16th, 2001 at 11:53 PM.
-
Aug 17th, 2001, 03:49 AM
#15
PowerPoster
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
-
Aug 17th, 2001, 08:08 AM
#16
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
-
Aug 17th, 2001, 08:15 AM
#17
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
-
Aug 17th, 2001, 08:19 AM
#18
PowerPoster
-
Aug 17th, 2001, 11:01 AM
#19
Frenzied Member
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

-
Nov 16th, 2001, 08:38 PM
#20
Hyperactive Member
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!!
-
Nov 16th, 2001, 08:43 PM
#21
-
Nov 16th, 2001, 08:46 PM
#22
PowerPoster
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
-
Nov 16th, 2001, 08:50 PM
#23
We could do better. Hey, Beach? {stuart}, Lets dig up one of your oldie moldies?
-
Nov 16th, 2001, 08:55 PM
#24
-
Nov 16th, 2001, 08:58 PM
#25
PowerPoster
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
-
Nov 16th, 2001, 09:01 PM
#26
-
Nov 16th, 2001, 09:05 PM
#27
PowerPoster
-
Nov 16th, 2001, 09:12 PM
#28
Dragged up per request!
-
Feb 7th, 2002, 01:06 PM
#29
Fanatic Member
It's threads like this one that make me love VB World so much!
-
Apr 12th, 2003, 09:12 PM
#30
Lively Member
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>
-
Apr 12th, 2003, 09:55 PM
#31
Hyperactive Member
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.
-
Apr 23rd, 2003, 02:56 AM
#32
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|