' Reading a Specific Line of Text from a Multi-Line Textbox
'
' Jon Vote, Idioma Software Inc.
'
'02/2002
'
' www.idioma-software.com
'
' 1) Create a new project. Form1 will be created by default.
' 2) Add a Textbox to the Form.
' 3) Set the Multiline property of the Textbox to TRUE.
' 4) Set the Scrollbars property of the Textbox to 2 - Verticle.
' 5) Add a Listbox below the Textbox.
' 6) Add a Command Button below the Listbox.
' 7) Add a Module to the Project.
' 8) Paste the following code into the declarations section of Form1.
' --- Begin code for Form1
Option Explicit
Private Sub Command1_Click()
Dim lngLineCount As Long
Dim i As Long
'We'll put the results in the list box
List1.Clear
'Get the number of lines in the textbox
lngLineCount = GetLineCount(Text1)
List1.AddItem "Number of lines: " & lngLineCount & " (0 to " & lngLineCount - 1 & ")"
List1.AddItem ""
'Iterate through each line
For i = 0 To lngLineCount - 1
List1.AddItem "Line " & CStr(i) & ": " & GetLine(Text1, i)
Next i
End Sub
Private Sub Form_Load()
Dim i As Integer
Me.Caption = "Reading a specific line from a multi-line textbox."
'Load some data into the textbox.
Text1.Text = "Joe" & vbCrLf & "Fred" & vbCrLf & "Mary" & vbCrLf & "Todd" & vbCrLf & "Betty" & vbCrLf
For i = 1 To 10
Text1.Text = Text1.Text & "This is some more text "
Next i
Command1.Caption = "Go"
Command1.Default = True
End Sub
' --- End code for Form1 ---
' 9) Paste the following code into Module1.
' --- Begin code for Module1
Option Explicit
Public Declare Function SendMessageAsString Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function SendMessageAsLong 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_GETLINE = 196
Public Const EM_GETLINECOUNT = 186
'Returns number of lines in Textbox txtText
Public Function GetLineCount(txtText As TextBox)
GetLineCount = SendMessageAsLong(txtText.hWnd, EM_GETLINECOUNT, 0, 0)
End Function
'Returns line number lngLineNumber from TextBox txtText
Public Function GetLine(txtText As TextBox, lngLineNumber As Long) As String
Dim lngMaxPossibleLength As Long
Dim intLowByte As Integer
Dim intHighByte As Integer
Dim strBuffer As String
Dim lngReturnCode As Long
'Use the length of text in the Textbox for the maximum possible length of a line
lngMaxPossibleLength = Len(txtText.Text)
'Calculate the maximum length as low byte, high byte
intLowByte = lngMaxPossibleLength And 255
intHighByte = Int(lngMaxPossibleLength / 256)
'Initialize the buffer
'Max length goes in first two bytes - low byte, high byte order
strBuffer = Chr$(intLowByte) & Chr$(intHighByte) & Space$(lngMaxPossibleLength - 2)
'Get the requested line of text from the Textbox
lngReturnCode = SendMessageAsString(txtText.hWnd, EM_GETLINE, lngLineNumber, strBuffer)
'lngReturnCode has length of buffer to return
GetLine = Left$(strBuffer, lngReturnCode)
End Function
' --- End code for Module1---