If you would like to make some extra cash for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html
Want to get up to speed on the latest Visual Basic programming? Includes Visual Basic 6 and Visual InterDev 6. Check out our training programs at http://www.codeoftheweek.com/vbtraining.html
In this issue we discuss how to get information about a multiline textbox that is not available using the built-in properties.
If you have any questions about using this module, let us know at questions@codeoftheweek.com
The cMultiLineTextBox class module contains several properties which allow you to get information such as the current line number and current column position within a standard multi-line textbox control. To accomplish these tasks it uses several API constants and the SendMessage API call.
To use this class you must set the TextBoxControl property equal to the multi-line textbox that is on your form. You can create multiple instances of this class if necessary.
Public Property Set TextBoxControl(txt As TextBox)
You must assign a TextBox control to this property in order to use the features of this class. It can be any text box on any form.
Public Property Get TextBoxControl() As TextBox
If you need to retrieve information about the text box this class is currently "managing" you can use this property.
Public Property Get LineCount() As Long
Returns the number of lines currently in the textbox specified by TextBoxControl. This line count is zero based and will change based on whether the scroll bar features are used or not. If the horizontal scroll bar feature is used the multi-line text box will not wrap a line and the line count will reflect that. If word wrap occurs it will consider a single line wrapped into two lines as two lines in the LineCount, not one.
Public Property Get CurrentLineNumber() As Long
Returns the line number the cursor is currently positioned at.
Public Property Get LineLength(Optional ByVal lLineNumber As Long = -1)
Returns the line length of the line number specified by lLineNumber. If it is not specified it will return the line length of the line the cursor is currently positioned at.
Public Property Get LinePosition()
Returns the position within the line that the cursor is currently positioned at.
The below sample assumes you have a form with a textbox called txtMulti. It should have the MultiLine property set to True. It also assumes you have a timer control called tmrStatus and a picture box called picStatus (which is aligned at the bottom of the screen). This sample is a basic text editor with line number and cursor positioning enabled. On Windows 95/98 it is limited to 32000 bytes of text.
An image of this sample is available at http://www.codeoftheweek.com/issues/graphics/simpleeditor.png
Dim oMultiLineTextbox As New cMultilineTextBox Private Sub Form_Load() Set oMultiLineTextbox.TextBoxControl = txtMulti tmrStatus.Interval = 250 ' 1/4 second tmrStatus.Enabled = True End Sub Private Sub tmrStatus_Timer() UpdateStatusBar End Sub Private Sub UpdateStatusBar() pictStatus.Cls pictStatus.Print "Line " & oMultiLineTextbox.CurrentLineNumber & " of " & oMultiLineTextbox.LineCount & _ " Position " & oMultiLineTextbox.LinePosition & " of " & oMultiLineTextbox.LineLength End Sub
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0111.html
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html