text object line info

By | 2002-06-01

The first function returns usefull information about text box objects. these include :
[Line count] = 0
[Cursor Position] = 1
[Current Line Number] = 2
[Current Line Start] = 3
[Current Line End] = 4
[Current Line Length] = 5
[Current Line Cursor Position] = 6
[Line Start] = 7
[Line End] = 8
[Line Length] = 9
The next function returns the text of a given line of a text box object.

Original Author: Damien McGivern

Inputs

Public Enum LineInfo
[Line count] = 0
[Cursor Position] = 1
[Current Line Number] = 2
[Current Line Start] = 3
[Current Line End] = 4
[Current Line Length] = 5
[Current Line Cursor Position] = 6
[Line Start] = 7
[Line End] = 8
[Line Length] = 9
End Enum
Public Function getLineInfo(txtObj As Object, info As LineInfo, Optional lineNumber As Long) As Long
Public Function GetLineText(txtObj As Object, lineNumber As Long) As String
‘// If lineNumber = 0 then current line’s text is given

API Declarations

Public Declare Function SendMessageLong Lib “USER32” Alias “SendMessageA” (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const EM_GETSEL As Long = &HB0
Public Const EM_SETSEL As Long = &HB1
Public Const EM_GETLINECOUNT As Long = &HBA
Public Const EM_LINEINDEX As Long = &HBB
Public Const EM_LINELENGTH As Long = &HC1
Public Const EM_LINEFROMCHAR As Long = &HC9
Public Const EM_SCROLLCARET As Long = &HB7
Public Const WM_SETREDRAW As Long = &HB

Code

'Author : Damien McGivern
'E-Mail : D_McGivern@Yahoo.Com
'Date : 30 Aug 1999
Option Explicit
Public Declare Function SendMessageLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const EM_GETSEL As Long = &HB0
Public Const EM_SETSEL As Long = &HB1
Public Const EM_GETLINECOUNT As Long = &HBA
Public Const EM_LINEINDEX As Long = &HBB
Public Const EM_LINELENGTH As Long = &HC1
Public Const EM_LINEFROMCHAR As Long = &HC9
Public Const EM_SCROLLCARET As Long = &HB7
Public Const WM_SETREDRAW As Long = &HB
Public Enum LineInfo
[Line count] = 0
[Cursor Position] = 1
[Current Line Number] = 2
[Current Line Start] = 3
[Current Line End] = 4
[Current Line Length] = 5
[Current Line Cursor Position] = 6
[Line Start] = 7
[Line End] = 8
[Line Length] = 9
End Enum
Public Function getLineInfo(txtObj As Object, info As LineInfo, Optional lineNumber As Long) As Long
Dim cursorPoint As Long
'//Record where the cursor is
cursorPoint = txtObj.SelStart
Select Case info
  Case Is = 0 ' = "lineCount"
   getLineInfo = SendMessageLong(txtObj.hWnd, EM_GETLINECOUNT, 0, 0&)
  Case Is = 1 ' = "cursorPosition"
   getLineInfo = (SendMessageLong(txtObj.hWnd, EM_GETSEL, 0, 0&) &H10000) + 1
  Case Is = 2 ' = "currentLineNumber"
   getLineInfo = (SendMessageLong(txtObj.hWnd, EM_LINEFROMCHAR, -1, 0&)) + 1
  Case Is = 3 ' = "currentLineStart"
   getLineInfo = SendMessageLong(txtObj.hWnd, EM_LINEINDEX, -1, 0&) + 1
  Case Is = 4 ' = "currentLineEnd"
   getLineInfo = SendMessageLong(txtObj.hWnd, EM_LINEINDEX, -1, 0&) + 1 + SendMessageLong(txtObj.hWnd, EM_LINELENGTH, -1, 0&)
  Case Is = 5 ' = "currentLineLength"
   getLineInfo = SendMessageLong(txtObj.hWnd, EM_LINELENGTH, -1, 0&)
  Case Is = 6 ' = "currentLineCursorPosition"
   getLineInfo = (SendMessageLong(txtObj.hWnd, EM_GETSEL, 0, 0&) &H10000) + 1 - SendMessageLong(txtObj.hWnd, EM_LINEINDEX, getLineInfo(txtObj, [Current Line Number]) - 1, 0&)
  Case Is = 7 ' = "lineStart"
   getLineInfo = (SendMessageLong(txtObj.hWnd, EM_LINEINDEX, (lineNumber - 1), 0&)) + 1
  Case Is = 8 ' = "lineEnd"
   getLineInfo = SendMessageLong(txtObj.hWnd, EM_LINEINDEX, (lineNumber - 1), 0&) + 1 + SendMessageLong(txtObj.hWnd, EM_LINELENGTH, (lineNumber - 1), 0&)
  Case Is = 9 ' = "lineLength"
   getLineInfo = (SendMessageLong(txtObj.hWnd, EM_LINEINDEX, lineNumber, 0&)) + 1 - (SendMessageLong(txtObj.hWnd, EM_LINEINDEX, (lineNumber - 1), 0&)) - 3
End Select
End Function
Public Function GetLineText(txtObj As Object, lineNumber As Long) As String
'// If lineNumber = 0 then current line's text is given
If lineNumber = 0 Then lineNumber = getLineInfo(txtObj, [Current Line Number])
'// Select text
Call SendMessageLong(txtObj.hWnd, EM_SETSEL, ((getLineInfo(txtObj, [Line Start], lineNumber)) - 1), ((getLineInfo(txtObj, [Line Start], lineNumber + 1)) - 1))
GetLineText = txtObj.SelText
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.