Hi Mike, I vb.net skill is above a newbie, and would probably
put myself into a 'good beginner' but I am unfamiliar with
EM_GETLINECOUNT. I went a different route and created
my own function to parse out the text and word wrap based
on an X amount of characters per line, incorporating into it the
enterkey, linefeeds, and tabs.
The simple method of detecting the word wrap points using an X amount of
characters per line, as you have suggested, will work only if you are using
a fixed width font in the TextBox, such as Courier New for example. To
correctly deduce the TextBox's word wrap positions when using a
proportionally spaced font, such as the default Microsoft Sans Serif or
Arial or Times New Roman or whatever in which the width of the character is
different for different characters, you will need to acurately take into
account the pixel width of the characters as drawn. Measuring character and
string pixel widths is of course easy to do using the VB functions provided,
but when doing so you must ensure that you are mimicing the behaviour of the
TextBox /exactly/ which again is possible but is not as straight forward as
it might at first seem and there are a number of factors to take into
account as mentioned briefly in my other response.
If you don't want to use the SendMessage method, which is very easy and
which does produce the correct result, then you could instead use the
straight VB.Net code at the link posted by James Hahn (which does not
currently work correctly) as a basis and you could build on it by adding
extra code to take into account the things I have mentioned, but personally,
if I were doing this job myself I would simply send the TextBox the
appropriate messages and effectively ask the TextBox itself where it wrapped
the lines.
By the way, you haven't said exactly why you are performing this task but if
it is so that you can print a block of text on another screen device exactly
as it is displayed in the TextBox then breaking up the block into its
displayed lines (as you are attempting to do) will work whichever method you
use to deduce the correct line wrap positions, or even by instructing the
Control when wrapping the text to measure its output on a different device
than the device on which it is rendering it (some Controls are capable of
performing this function). But if you intend to produce the output on a
device other than the screen (a printer for example) then it will work only
insofar as the individual lines themselves will contain the same words. This
of course may be suitable for your needs. However, the lines of text
themselves will not be exactly the same logical width on the screen as they
are on the printer. If this isn't a problem for you for the job you are
performing then that's okay of course. However, if you do see it as a
problem then you will need to add code to take account of it and to adjust
things so that everything does line up correctly.
The failure of the width of the same line of characters to be the same on
different devices is due to a number of factors. One problem is the fact
that the font size is usually limited to a point size (a vertical
measurement) that produces a whole number of pixels on the specific device
you are using it with, and so in many cases you cannot actually achieve the
same font point size on the printer as you can on the screen since their
device pixels have a different size. Another problem is that even when it is
possible to achieve the same font point size on both devices there is still
the fact that all individual characters (most of which have different widths
in a proportionally spaced font) are also usually restricted to a character
cell width that equates to a whole number of device pixels, and so the
actual logical width of a line of characters is extremely unlikely to be the
same on the printer as it is on the display, even in cases where the font
face and point size is exactly the same.
These problems can of course be overcome in all sorts of different ways by
altering the spacing between individual characters and words to achieve the
same overall printed width on both devices, but if it isn't a problem for
the job you are actually doing then it might not be worth going to the extra
trouble to do it.
Mike