Also available is a Project:
http://www.activevb-archiv
Main Topics
Browse All TopicsI have a form with a few text fields, one of which is multiline. It truncates off at the end , as the user could enter more than the width of the page. I need to see "source code" on how to format the txtDescription.text so that it will not be cut off.
'/////////////////////////
270 Printer.Print Tab(0); Label10;
280 Printer.Print Tab(0); txtDescription.Text
'/////////////////////////
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Also available is a Project:
http://www.activevb-archiv
try this
Following Code also sets the alignment..
'Add this to your Declarations Section:
Public Declare Function SetTextAlign Lib "gdi32.dll" (ByVal hdc As Long, ByVal wFlags As Long) As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wmsg As Long, ByVal wparam As Long, lparam As Any) As Long
Public Const TA_CENTER = 6
Usage
'Create a Form with a Command Button and a Multiline Textbox
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim ta As Long
Dim TextLines As Long
Dim TextBuff As String
Dim CharRet As Long
'Add this to your print code!' Center text on printer object
ta = SetTextAlign(Printer.hdc, TA_CENTER)
Printer.CurrentY = (Printer.ScaleHeight / .Parent.ScaleHeight) * .Top
' Get number of lines in text box
TextLines = SendMessage(.hwnd, &HBA, 0, 0)
' Extract & print each line in TextBox
For i = 0 To TextLines - 1
TextBuff = Space(1000)
Printer.CurrentX = (Printer.ScaleWidth / .Parent.ScaleWidth) * (.Left + (.Width / 2))
' Setup buffer for the line!
Mid(TextBuff, 1, 1) = Chr(79 And &HFF)
Mid(TextBuff, 2, 1) = Chr(79 \ &H100)
CharRet = SendMessage(.hwnd, &HC4, i, ByVal TextBuff)
Printer.Print Left(TextBuff, CharRet)
Next i
' Reset alignment back to original setting
ta = SetTextAlign(Printer.hdc, ta)
End Sub
Use the DrawText API:
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
' DrawText() Format Flags
Const DT_TOP = &H0
Const DT_LEFT = &H0
Const DT_CENTER = &H1
Const DT_RIGHT = &H2
Const DT_VCENTER = &H4
Const DT_BOTTOM = &H8
Const DT_WORDBREAK = &H10
Const DT_SINGLELINE = &H20
Const DT_EXPANDTABS = &H40
Const DT_TABSTOP = &H80
Const DT_NOCLIP = &H100
Const DT_EXTERNALLEADING = &H200
Const DT_CALCRECT = &H400
Const DT_NOPREFIX = &H800
Const DT_INTERNAL = &H1000
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
dim TextBox as RECT
' define location of box on page in twips
TextBox.left =
TextBox.right =
TextBox.top =
TextBox.bottom =
'
lFormat = DT_LEFT + DT_NOPREFIX + DT_WORDBREAK + DT_NOCLIP
lTextLen = Len(sText)
lRet = DrawText(Printer.hDc, sText, lTextLen, TextBox, lFormat)
anv: Tried that from andrea vb's site and it did not work. I don't want to center the text, I want to wrap it when it prints.
vb Elmar: Do not understand comments in German and the code sample does not print.
Bob: No, you don't understand the question. It is how to print the multiline textbox
Bryan: I get an error when running your code on line
Textbox=
invalid outside procedure
--------------------------
I did solve the problem and found code on Microsoft's website and will ask to PAQ this question:
This is the correct code to print a mulitline textbox:
add a label1 to form, 1 command, 1 textbox .
#If Win32 Then
Private Declare Function SendMessageAsLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageAsString Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As String) As Long
Dim Buffer As String
Dim resizing As Integer
Const EM_GETLINE = 196
Const EM_GETLINECOUNT = 186
#Else
Private Declare Function SendMessage% Lib "user" _
(ByVal hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam As Any)
Dim Buffer As String
Dim resizing As Integer
Const EM_GETLINE = &H400 + 20
Const EM_GETLINECOUNT = &H400 + 10
#End If
Const MAX_CHAR_PER_LINE = 80 ' Scale this to size of text box.
Private Sub Form_Load()
' Size form relative to screen dimensions.
' Could define all in move command but recursive definition causes
' extra paints.
frmworkorder.Width = Screen.Width * 0.8
frmworkorder.Height = Screen.Height * 0.6
frmworkorder.Move Screen.Width \ 2 - frmworkorder.Width \ 2, _
Screen.Height \ 2 - frmworkorder.Height \ 2
End Sub
Private Sub Form_Resize()
resizing = -1 ' Global flag for fGetLineCount function call.
' Dynamically scale and position the controls in the form.
' This code also is executed on first show of form.
'txtDescription.Move 0, 0, frmworkorder.Width, frmworkorder.Height \ 2
txtDescription.SelStart = txtDescription.SelStart
Command1.Move frmworkorder.Width \ 2 - Command1.Width \ 2, _
frmworkorder.Height - frmworkorder.Height \ 4
aGetLineCount.Move frmworkorder.Width \ 2 - Command1.Width \ 2, _
txtDescription.Height
X% = fGetLineCount() ' Update to reflect change in text-box size.
resizing = 0
End Sub
Private Sub Command1_Click()
OK = -1
NL$ = Chr$(13) + Chr$(10)
ndx& = fGetLineCount&()
For N& = 1 To ndx&
Buffer = fGetLine(N& - 1)
Printer.Print Buffer ' ...or print to the screen.
Next N&
' Print a line...
Buffer = fGetLine(Val(response$) - 1)
Printer.Print Buffer ' ...or print to the screen.
Printer.EndDoc
End Sub
Private Function fGetLine$(LineNumber As Long)
' This function fills the buffer with a line of text
' specified by LineNumber from the text-box control.
' The first line starts at zero.
byteLo% = MAX_CHAR_PER_LINE And (255) '[changed 5/15/92]
byteHi% = Int(MAX_CHAR_PER_LINE / 256) '[changed 5/15/92]
Buffer$ = Chr$(byteLo%) + Chr$(byteHi%) + Space$( _
MAX_CHAR_PER_LINE - 2)
#If Win32 Then
X = SendMessageAsString(txtDes
#Else
X = SendMessage(txtDescription
#End If
fGetLine$ = Left$(Buffer$, X)
End Function
Private Function fGetLineCount&()
' This function will return the number of lines
' currently in the text-box control.
' Setfocus method illegal while in resize event,
' so use global flag to see if called from there
' (or use setfocus before this function call in general case).
#If Win32 Then
lcount = SendMessageAsLong(txtDescr
#Else
lcount = SendMessage(txtDescription
#End If
aGetLineCount.Caption = "GetLineCount = " + Str$(lcount)
fGetLineCount& = lcount
End Function
Private Sub txtDescription_Change()
X% = fGetLineCount() '* Update label to reflect current line
End Sub
Business Accounts
Answer for Membership
by: vb_elmarPosted on 2004-12-15 at 15:51:48ID: 12835946
You have to split each line, and putting each line in an array.
-If the Textbox has a big content, you can send each line to the printer.
Try this:
' "Form1"
' "List1"
' "Command1"
' "Text1"
' "Label2"
' "Label1"
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
Const EM_GETLINECOUNT = &HBA
Const EM_LINEFROMCHAR = &HC9
Public Function GetTextBoxZeilen(Tbox As TextBox, Zeilen() As String) _
As Long
'liefert Anzahl der dargestellten Zeilen einer Textbox und übergibt die
'Zeileninhalte in einem StringArray
Dim LineNo As Long
Dim LineLast As Long
Dim LineStart As Long
Dim i As Long
'Anzahl der dargestellten Zeilen ermitteln
LineNo = SendMessage(Tbox.hwnd, EM_GETLINECOUNT, 0, 0&)
' Array soweit erweitern, das alle Zeilen hineinpassen
ReDim Zeilen(LineNo)
For i = 0 To Len(Tbox.Text) - 1
'Zeilennummer für aktuelles Zeichen
LineNo = SendMessage(Tbox.hwnd, EM_LINEFROMCHAR, i, 0&)
If LineNo <> LineLast Then
'Zeilenwechsel, Zeichen an Zeile zuweisen
Zeilen(LineLast) = Mid(Tbox.Text, LineStart + 1, i - LineStart)
LineLast = LineNo
LineStart = i
End If
Next
'letzte Zeile
Zeilen(LineLast) = Mid(Tbox.Text, LineStart + 1, i - LineStart)
'CrLf´s (Zeilenumbrueche) entfernen
For i = LBound(Zeilen) To UBound(Zeilen)
Zeilen(i) = Replace(Zeilen(i), vbCrLf, "")
Next
' Rückgabe der Zeilenanzahl
GetTextBoxZeilen = UBound(Zeilen)
End Function
Private Sub Command1_Click()
' Das Array, das jede Zeile einzeln enthält
Dim einzelnezeilen() As String
Dim i
' Aufrufen der Funktion und übergabe der Zeilenanzahl an Label1
Label1 = GetTextBoxZeilen(Text1, einzelnezeilen())
' Alle einzelnen Zeilen durchgehen und
For i = 0 To UBound(einzelnezeilen)
' in List1 hinzufügen
List1.AddItem einzelnezeilen(i)
Next i
End Sub