Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing multi-line texbox

Status
Not open for further replies.

deepsheep

Programmer
Sep 13, 2002
154
CA
I have a paper form that I am converting to a electronic one. Because the people that will be using it are afraid of computers, I am trying to keep the layout as close as possible to the paper version. Stick to what the user knows.
Most of the work is done, I'm just trying to get the printing part working and I've run into one problem that I can't seem to solve.

Some of the Labels and textboxes use more than one line on the electonic form, but print only on one line. This screws up the formatting. Is there an easy way to split it onto two (or more) lines, or do I have to manually split each field and manually print each line of the field?
Thanks (again)!
 
how about something like this (if I understand your goal correctly)?

dim yourstring as varchar(8000) ' this will be the eventual variable with carriage returns built in for formatting

yourstring = ""

For I = 1 To len(myTextbox.text) step 80 (or however many characters you want before carriage return]

yourstring = yourstring & mid( myTextbox.text , 1 + counter, 80) & chr(13)


Next I
 
Are you actually printing the form?

It might be a whole lot cleaner to take the "print" action and format a nice layout in a RichTextBox and then print that.
 
Or you may try 'pouring' your string onto the paper with something like this;

const lmargin = 2
const rmargin = 10

printer.scalemode vbcentimeters

printer.currentx=lmargin
for i = 1 to len(MyLongString$)
printer.print mid$(MyLongString$,i,1);
if printer.currentx=>rmargin then
printer.print
printer.currentx=lmargin
end if
next

Hugh,
 
One other possibility would be to send the textbox an EM_FMTLINES message
 
Sorry I didn't get back sooner it was the weekend. I'll try your suggestions and let you know my results.
Thanks.
 
None of your approcehes quite worked the way I was wanting. In my search for more information I came across a site that gave me the outcome I wanted. For the next person :
It ended up being a fancier vesion of some of your idea. Thanks anyway!
 
Which firstly is really just a slightly more sophisticated version of HughLerwill's suggestion, secondly has real problems if there is any text wider than the textbox, and thirdly just seems a lot of effort when you can get the textbox to do most of the work for you ... e.g
Code:
[blue]Option Explicit
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 Const EM_FMTLINES = &HC8

Private Sub fmtTBPrint(ctlText As TextBox, Optional Left As Single = 0, Optional Top As Single = 0) 'Top and Left define top left position in the current scalemode of the printer
    Dim strText As String
    Dim textline As Variant
    Dim OldFont As Font
    
    SendMessage ctlText.hwnd, EM_FMTLINES, 1, 0&
    
    ' Save current printer font and switch to whatever font is being used in the textbox
    Set OldFont = Printer.Font
    Set Printer.Font = ctlText.Font
    
    Printer.CurrentY = Top
    For Each textline In Split(Text1.Text, vbCr & vbCrLf)
        Printer.CurrentX = Left
        Printer.Print textline
    Next
    SendMessage ctlText.hwnd, EM_FMTLINES, 1, 0&
    
    ' Switch back to original font
    Set Printer.Font = OldFont
    Printer.ScaleMode = OldScale
End Sub
[/blue]
 
Oops - the second SendMessage should read:

SendMessage ctlText.hwnd, EM_FMTLINES, 0, 0&
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top