Wrap text

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.
AzmodanAsked:
Who is Participating?
 
rspahitzCommented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

 Why not to use the SpinButton with Edit box

Best regards
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
glass_cookieCommented:
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
 
AzmodanAuthor Commented:
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
 
AzmodanAuthor Commented:
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
 
rspahitzCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.