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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number of lines or total height of lines in rtf 1

Status
Not open for further replies.

Hokkie

MIS
Nov 21, 2001
77
NL
Hi,

is there a (simple) way to determine how many lines there are in a rich text box that has multiline set to true and only one fontsize (though it may have some styles like bold and italic)?

If I use the RichTextBox.TextHeight property, it seems to return the number of paragraphs * textheight. A paragraph can be split into multiple lines with word-wrap. I need to find a way to calculate the number of lines (including blank lines) so that I can resize the rich text box to fit all the text inside (I resize it vertically).

I can do this by adding all the separate words in a rtf to a string, then checking after every word to see if the textwidth exceeds the width of the rtf. Then back-up one word and loop untill last word. But I'd have to do that for every keystroke the user gives me. Wouldn't that be too slow?

What I do now is to divide the width of a paragraph by the width of the rtf, but that does NOT take into account lines that end before the very right margin of the rtf box. On some occasions this creates a textbox that is higher/longer than it should be.

Any ideas how to tackle this? Anything at all would be welcome!

Hokje


when there's too much something is missing
 
Simple? Well, not entirely - but try my code from a year ago in thread708-352069
 
strongm,

heyhey it works! I noticed you already received like a zillion stars for it in that thread, but here's an additional one. Thanx!

Hokje
 
I've found a snippet in Total VB SourceBook by FMS Inc., that returns the number of lines in a (rich)textbox. I post my own implementation of this because the VB SourceBook's code is copyrighted.


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_GETLINECOUNT = &HBA

Public Function GetLineCount(txtIn As RichTextBox) As Integer

' Determines the number of lines in a
' multi-line (rich) text box
' Replace txtIn As RichTextBox with txtIn As TextBox
' to use this function on a regular textbox.

Dim lngNumLines As Long

lngNumLines = SendMessage(txtIn.hwnd, EM_GETLINECOUNT, 0, ByVal 0&)
GetLineCount = lngNumLines

End Function


The above code works fast (faster than strongm's code in the thread mentioned in his post), but it only gives you the number of lines, NOT the height of the text. But it suits my needs perfectly.
 
Yep, it will be a lot faster. And if your requirement stays that the RTB only ever has one font at one size then, as you say, it will suit your need perfectly (and show I should have read your spec a little more closely ;-))

For future readers it is worth pointing out, however, that the moment you start mixing fonts, their sizes and their styles in the RTB, the simple technique of counting the number of lines (EM_GETLINECOUNT) and then multiplying by the height of the text (eg. Form.TextHeight("DummyText")) will become inaccurate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top