Extending the TextBox Control
As we all know the strength of VB is it's simplicity. Just draw some controls onto a form set some properties during design time, write some "glue code" and you're done (well, almost anyway). VB also provides us with ready to go controls that are the same as Windows own controls (i.e. TextBox, ListBox, CommandButton and so on) or are they?
Windows own controls have more properties then those available in VB. Properties that Microsoft was too lazy to include or just didn't think a VB developer needed.
Take an ordinary multiline textbox for example. It is almost the same as a Windows edit box except for the lack of several properties. An edit box has a property which the VB developer can use to retrieve the number of lines of text it contains. You are also able to get the index of the first visible line (almost like the TopIndex property of a list box) for example.
But is there a way to harness the power of these hidden properties? Luckily there is! With the help of the SendMessage API function you can extend an ordinary textbox so you can treat the lines of text as an array of strings. This is very useful when you want to parse the text. Let's say that you want to create an HTML editor and you want to add color coding (just like the editor in VB itself). Wouldn't it be very useful then if you could extract just the line of text the text caret is on and examines that text for the HTML tags you want to add colors to? Of course it would! But how do you add colors to a textbox you ask? Well, actually you can't. But the code I'm going to show you could as easily be used on a rich text box as well as a standard textbox.
Let's go to work!