Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #111
Online Version at http://www.codeoftheweek.com/membersonly/bi/0111.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Multiline Text Box Class

Earn REAL money surfing the web!

If you would like to make some extra cash for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Six months of VB Training for only $49.99

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

Requirements

In this Issue

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

cMultiLineTextBox

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.

Properties

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.

Sample Usage

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

Source Code

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


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com