Author : | Damien McGivern |
Date Written : | 16/09/99 |
E-Mail : | D_McGivern@Yahoo.com |
Web Site : | http://geocities.datacellar.net/d_mcgivern/Index.htm |
TxtObjInfo : | This module contains two usefull functions : TxtLineInfo : Returns usefull info about a text box GetLineText : Returns the text of a given line give no line and it returns text of current line. I have included the list of functions and subs in this examlpe. |
Functions : | getLineInfo GetLineText |
(To highlight code click repeatedly quickly) Option Explicit '// Usefull functions when using text boxes or rich text boxes 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 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, (SendMessageLong(txtObj.hwnd, EM_LINEFROMCHAR, -1, 0&)), 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, Optional lineNumber As Long) As String Dim cursorPoint As Long '//Record where the cursor is cursorPoint = txtObj.SelStart 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 txtObj.SelStart = cursorPoint End Function '