Determining the number of lines inside a TextBox control when in wordwrap-enabled mode

For .NET Compact Framework (Version 1.0), I am wondering how to determine the number of lines in a TextBox control when it is in automatic wordwrap mode.

Alternatively, how do I detect the minimum bounding rectangle for a TextBox control in order to display all text visibly without clipping?

I tried to do this, and I can do this under regular .NET but I can't do so on Compact Framework. Are there any workarounds I can do?

Thanks,
Mark Rejhon

[456 byte] By [MarkRejhon] at [2008-2-7]
# 1
Trying to figure out workarounds...

Is there an equivalent of the Win32 API call DrawText( ) with the DT_CALCRECT flag? Or even GetTextExtentExPoint( ) ? Any way to call lower level WIN CE API calls from Compact Framework?

MarkRejhon at 2007-9-8 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

Problem solved. I accomplished the procedure of determining the height of a wrapped string drawn to a control in Compact Framework 1.0.

The solution is AlexY's method here:
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=18914

MarkRejhon at 2007-9-8 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3
There is a EM_GETLINECOUNT message that you can send to a TextBox and get a number of lines in it.

-Alex

AlexY at 2007-9-8 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
MeasureStringExtend worked very well but sometimes I needed to display a few kilobytes of read-only text (which was rare, but due to the nature of the application I am working with). This is extremely slow.

I came up with a creative solution for PocketPC (2002 and later) to copy a Label to a temporary TextBox, do the EM_GETLINECOUNT method, and then size the Label according to the size of the TextBox. I came up with a routine that counted the number of pixels of margin around a textbox (which is 6 pixels normally, 3 pixels on each edge), so that I could more instantly count the lines of text in huge amount of text (i.e. 10 kilobytes)

However, for SmartPhones, EM_GETLINECOUNT does not seem to work (SmartPhone 2003), so for SmartPhones, I greatly improved AlexY's method of MeasureStringExtend( ) so that it was pixel-perfect even for 18 kilobytes of word-wrapped text with unexpected control characters including linefeeds and tabs. (Although I had to convert tabs to 8 spaces first). Very slow to count the number of lines in 18 kilobytes of text, but it came up with the correct exact number of lines.

Further improvements to MeasureStringExtend could be done, to speed it up such as making a length approximation and counting upwards/downwards from it, rather than doing it one character at a time, or even using a lookup table on character widths that's dynamically updated everytime a new character is found, but this would be overkill...

It was a big challenge staying away from CF 2.0, but I've managed to stay with CF 1.0 and at the same time, having the same executable run PPC2002, WM2003, WM2003SE, and SmartPhone 2003 without recompiling. Now to figure out how to make an installer that installs on any of those...

MarkRejhon at 2007-9-8 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...