Solved

Wrap text

Posted on 2002-05-09
7
172 Views
Last Modified: 2010-05-02
I need to print wraped text. How do I do that? Because a hidden textbox cannot be BitBlted...
I don't want to evaluate the length character by character.
0
Comment
Question by:Azmodan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6998419
Hello

 Why not to use the SpinButton with Edit box

Best regards
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6998428
oppppppps, plz ignore my last comment

I was opening two questions at the same time, so I post my comments in the wrong question

 i'm so sorry
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6998520
Hi!

How about this (this is what I normally do)...

Plac the textbox on an empty form with both the textbox and form set to borderstyle = none and their backcolor, white.

Remove all scrollbars on the textbox if any.  Then, use a mdi form to load your white form (which must be set to mdichild = true.  Sie your form to how much of the form that you want to print on paper.

After all these has been done, simply use this code:

WhiteForm.PrintForm  'Or whatever is the name of your form)

This will print whatever that's displayed on the form.

That's it!

glass cookie : )
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Azmodan
ID: 6998648
i thought of a solution
I put the text in a textbox multiline, let it wrap the text, and than read with API line by line from the textbox
0
 

Author Comment

by:Azmodan
ID: 6998840
But i can't get the API to work
res = SendMessageLong(Text1.hwnd, EM_GETLINE, 1, VarPtr(buff))
it crashes every time.
In the help it says that the first word of the buff must be the maximum length of the text read.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 6999266
Although this is not a perfect solution, you could take the text length and divide it by the output area length to determine how much can print, then print that portion and repeat for the remainder of the text.

Example:
Suppose that you have the text "This is a test of how to wrap text into a printable area." with font size Arial-12

Now let's say that your display area (form, picturebox, whatever) has a scalewidth of 150 pixels (2375 twips)

Using the display area (I'll use a form) get the textwidth and divide by the display width:

  Dim sglTextWidth As Single
  Dim sglFormWidth As Single
  Dim intRows As Integer
 
  sglTextWidth = Form1.TextWidth(Label1.Caption)'365
  sglFormWidth = Form1.ScaleWidth'150
  intRows = Int(sglTextWidth / sglFormWidth + 0.99)'3

From this, you can pull off the first block of characters (in this case 1/3 = 1/intRows) and print them.

  Form1.Print Left$(Label1.Caption, Len(Label1.Caption) / intRows)

And you can repeat this for each of the blocks.

--
The problems are:
1) Since the blocks will not actually fit exactly, the fraction will likely be slightly off.  In the above case, the actual fraction is 2.43 so taking 1/3 actually gives you less than you want.

2) This truncation will not take words into account.  To do that, you'll have to search backwards from the end of the substring to the previous word delimiter (space, punctuation, other(?))

If you'd like me to pursue this with you, let me know.

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6999281
Anyway, here's the simple wrapping code, without any consideration for "words".  Add a label with the text to print (in your case, replace Label1.Caption with your hidden textbox's name and text property)  Also, replace Form1 with the name of your desired output area.  I'm initiating the process with a button (Command1) click.

Private Sub Command1_Click()
  Dim sglTextWidth As Single
  Dim sglFormWidth As Single
  Dim sglLineLength As Single
  Dim intRows As Integer
  Dim strText As String
 
  sglTextWidth = Form1.TextWidth(Label1.Caption)
  sglFormWidth = Form1.ScaleWidth
  sglLineLength = sglTextWidth / sglFormWidth
  intRows = Int(sglLineLength + 0.99)
  strText = Label1.Caption
  While strText <> vbNullString
    Form1.Print Left$(strText, Len(Label1.Caption) / sglLineLength)
    strText = Mid$(strText, Len(Label1.Caption) / sglLineLength + 1)
  Wend
End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question