The Windows constant _EM_GETLINECOUNT is the message used to retrieve the number of lines from a textbox.
CallDLL #user32, "SendMessageA", _
hText asUlong, _ ' handle of the textbox
_EM_GETLINECOUNT asLong, _ ' message to be passed0asLong, _ ' no further information to be passed0asLong, _ ' no further information to be passed
result asLong' Windows returns the number of lines
Here is a demo to get the number of lines in a textbox. The mainwindow is kept visible for the printing of returned information. You may need to reposition your GUI window to see this information.
' Define a textbox
Textbox #main.tb, 10, 20, 150, 68' Stylebits for word wrapping and vertical scrolling
Stylebits #main.tb, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0' Define text
demoText$ ="All Gaul is divided into three parts, one of "+ _
"which the Belgae inhabit, the Aquitani another, those who "+ _
"in their own language are called Celts, in our Gauls, the third. "+ _
"All these differ from each other in language, customs, and laws."
Open "Lines of Multiline Textbox"for Window as #main
#main, "Trapclose XbyTrap"
#main, "Font Times_New_Roman 10 bold"' Print the text in the textbox
#main.tb, demoText$
' Get the handle of the textbox
hText=hWnd(#main.tb)' Pass the required arguments to SendMessageA API
nLines = ReturnedMessage(hText, _EM_GETLINECOUNT, 0, 0)
Print "There are ";nLines;" lines in this textbox."
Wait
' Program ends hereSub XbyTrap handle$
Close #handle$
EndEndSub' SendMessageA FunctionFunction ReturnedMessage(handle, Msg, wParam, lParam)
CallDLL #user32, "SendMessageA", _
handle asUlong, _
Msg asLong, _
wParam asLong, _
lParam asLong, _
ReturnedMessage asLongEndFunction
Get the Number of Characters in Each Line of Text
SendMessageA will not return the actual text string. Instead, it returns the number of characters in that string. This next demo is the same as the previous demo, with a function added to get the number of characters at the end of each line. It is important to recognize that the function does not return the number of characters in that line, but the number of characters from the beginning of the text to the end of that line. Again using the API call SendMessageA, the handle of the textbox is passed as well as the message _EM_LINEINDEX. SendMessageA also needs to know which line is being questioned. The first available extra information argument, wParam, passes the line number to be queried. There is no additional information needed, so the second informational argument, lParam, remains 0.
CallDLL #user32, "SendMessageA", _
hText asUlong, _ ' handle of the textbox
_EM_LINEINDEX asLong, _ ' message to be passed
lineNumber asLong, _ ' the queried line number0asLong, _ ' no further information to be passed
result asLong' Windows returns the number of lines
Since the last line is treated a little differently, the loop must terminate at the next to the last line For i = 1 to nLines - 1. The code for the last line immediately follows the loop.
' Define a textbox
Textbox #main.tb, 10, 20, 150, 68' Stylebits for word wrapping and vertical scrolling
Stylebits #main.tb, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0' Define text
demoText$ ="All Gaul is divided into three parts, one of "+ _
"which the Belgae inhabit, the Aquitani another, those who "+ _
"in their own language are called Celts, in our Gauls, the third. "+ _
"All these differ from each other in language, customs, and laws."
Open "Lines of Multiline Textbox"for Window as #main
#main, "Trapclose XbyTrap"
#main, "Font Times_New_Roman 10 bold"' Print the text in the textbox
#main.tb, demoText$
' Get the handle of the textbox
hText=hWnd(#main.tb)' Pass the required arguments to SendMessageA API
nLines = ReturnedMessage(hText, _EM_GETLINECOUNT, 0, 0)
Print "There are ";nLines;" lines in this textbox."' Get the total number of characters at the end of each lineFor i =1to nLines -1
nCharacters = ReturnedMessage(hText, _EM_LINEINDEX, i, 0)
Print "Line #";i;": ";nCharacters;" Characters"Next i
Wait
' Program ends hereSub XbyTrap handle$
Close #handle$
EndEndSub' SendMessageA FunctionFunction ReturnedMessage(handle, Msg, wParam, lParam)
CallDLL #user32, "SendMessageA", _
handle asUlong, _
Msg asLong, _
wParam asLong, _
lParam asLong, _
ReturnedMessage asLongEndFunction
How will knowing what the total number of characters reveal the actual number of characters in each line? By keeping track of how many characters were used in preceding lines, the number of characters already assigned is known. Subtract that number from the number of characters up to and including the selected line. The difference is the number of characters in just the selected line. Start the position, startPos, as 1.
' Define a textbox
Textbox #main.tb, 10, 20, 150, 68' Stylebits for word wrapping and vertical scrolling
Stylebits #main.tb, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0' Define text
demoText$ ="All Gaul is divided into three parts, one of "+ _
"which the Belgae inhabit, the Aquitani another, those who "+ _
"in their own language are called Celts, in our Gauls, the third. "+ _
"All these differ from each other in language, customs, and laws."
Open "Lines of Multiline Textbox"for Window as #main
#main, "Trapclose XbyTrap"
#main, "Font Times_New_Roman 10 bold"' Print the text in the textbox
#main.tb, demoText$
textLength = Len(demoText$)' Get the handle of the textbox
hText=hWnd(#main.tb)' Pass the required arguments to SendMessageA API
nLines = ReturnedMessage(hText, _EM_GETLINECOUNT, 0, 0)
Print "There are ";nLines;" lines in this textbox"' The starting position is 1
startPos =1For i =1to nLines -1' Get the total number of characters in each line
nCharacters = ReturnedMessage(hText, _EM_LINEINDEX, i, 0)' The characters in that line start with startPos and continue through to nCharacters
Print "Line ";i;": Characters ";startPos;" - ";nCharacters
' startPos moves up to the nCharacters pos for the next line
startPos = nCharacters +1Next i
' Get the number of characters in the last line by subtracting the nCharacters' found thus far from the Length of the entire text
nCharacters = textLength - nCharacters
Print "Line ";i;": Characters ";startPos;" - ";textLength
'Wait for button to be pushed
Wait
' Program ends hereSub XbyTrap handle$
Close #handle$
EndEndSub' SendMessageA FunctionFunction ReturnedMessage(handle, Msg, wParam, lParam)
CallDLL #user32, "SendMessageA", _
handle asUlong, _
Msg asLong, _
wParam asLong, _
lParam asLong, _
ReturnedMessage asLongEndFunction
Liberty BASIC's Mid$() Function
Now that the starting and ending character position number is known, the Liberty BASIC Mid$() function can be used to extract that one line. The Mid$() function has 3 elements
The whole text to be parsed (demoText$)
The starting position (startPos)
The number of characters to be extracted from the starting position to the right (nCharacters - startPos)
A simple demo of the Liberty BASIC Mid$() function -
txt$ ="trouble"
m$ =Mid$(txt$, 4, 3)
Print m$
End
Executing this code prints ubl, where u is the character in the fourth position of the string trouble, and ubl are the three characters in sequence beginning in this fourth position.
In this textbox demo, startPos becomes the start position and nCharacters - startPos becomes the sequence range of characters. Here is the code again, this time extracting and printing each line of text.
' Define a textbox
Textbox #main.tb, 10, 20, 150, 68' Stylebits for word wrapping and vertical scrolling
Stylebits #main.tb, _WS_VSCROLL OR _ES_MULTILINE, _ES_AUTOHSCROLL, 0, 0' Define text
demoText$ ="All Gaul is divided into three parts, one of "+ _
"which the Belgae inhabit, the Aquitani another, those who "+ _
"in their own language are called Celts, in our Gauls, the third. "+ _
"All these differ from each other in language, customs, and laws."
Open "Lines of Multiline Textbox"for Window as #main
#main, "Trapclose XbyTrap"
#main, "Font Times_New_Roman 10 bold"' Print the text in the textbox
#main.tb, demoText$
textLength = Len(demoText$)' Get the handle of the textbox
hText=hWnd(#main.tb)' Pass the required arguments to SendMessageA API
nLines = ReturnedMessage(hText, _EM_GETLINECOUNT, 0, 0)
Print "There are ";nLines;" lines in this textbox"' The starting position is 1
startPos =1For i =1to nLines -1' Get the total number of characters in each line
nCharacters = ReturnedMessage(hText, _EM_LINEINDEX, i, 0)' Print that line of text using the Mid$() function
Print Mid$(demoText$, startPos, nCharacters - startPos)' startPos moves up to the nCharacters pos for the next line
startPos = nCharacters +1Next i
' Get the number of characters in the last line by subtracting the nCharacters' found thus far from the Length of the entire text
nCharacters = textLength - nCharacters
Print Mid$(demoText$, startPos, nCharacters)'Wait for button to be pushed
Wait
' Program ends hereSub XbyTrap handle$
Close #handle$
EndEndSub' SendMessageA FunctionFunction ReturnedMessage(handle, Msg, wParam, lParam)
CallDLL #user32, "SendMessageA", _
handle asUlong, _
Msg asLong, _
wParam asLong, _
lParam asLong, _
ReturnedMessage asLongEndFunction
You may never need to extract single lines of text in a multiline textbox, but at least you know you can.
Retrieving Information from Textboxes
The Native Liberty BASIC Command
Liberty BASIC has a native command for retrieving textbox contents -
In most instances, this is all the code programs require.
A Multiline Textbox
There are times when a multiline, word wrapping textbox may be preferred over the native Liberty BASIC texteditor. See also An Improved Stylebits Textbox.
There may also be a need to
These functions are possible with an API call to #user32, "SendMessageA".
CallDLL #user32, "SendMessageA"
The SendMessageA function sends a specified messsage to a window or a control. The arguments to be passed include
- handle The handle of the window or control ULONG
- Msg The specific message LONG
- wParam additional information about the message LONG
- lParam additional information about the message LONG
When the message is received, the return value specifies the result of the message processing.Visit the MSDN Library for more information about SendMessage.
Get the Number of Lines in the Textbox
The Windows constant _EM_GETLINECOUNT is the message used to retrieve the number of lines from a textbox.
Here is a demo to get the number of lines in a textbox. The mainwindow is kept visible for the printing of returned information. You may need to reposition your GUI window to see this information.
Get the Number of Characters in Each Line of Text
SendMessageA will not return the actual text string. Instead, it returns the number of characters in that string. This next demo is the same as the previous demo, with a function added to get the number of characters at the end of each line. It is important to recognize that the function does not return the number of characters in that line, but the number of characters from the beginning of the text to the end of that line. Again using the API call SendMessageA, the handle of the textbox is passed as well as the message _EM_LINEINDEX. SendMessageA also needs to know which line is being questioned. The first available extra information argument, wParam, passes the line number to be queried. There is no additional information needed, so the second informational argument, lParam, remains 0.
Since the last line is treated a little differently, the loop must terminate at the next to the last line For i = 1 to nLines - 1. The code for the last line immediately follows the loop.
How will knowing what the total number of characters reveal the actual number of characters in each line? By keeping track of how many characters were used in preceding lines, the number of characters already assigned is known. Subtract that number from the number of characters up to and including the selected line. The difference is the number of characters in just the selected line. Start the position, startPos, as 1.
Liberty BASIC's Mid$() Function
Now that the starting and ending character position number is known, the Liberty BASIC Mid$() function can be used to extract that one line. The Mid$() function has 3 elements
A simple demo of the Liberty BASIC Mid$() function -
Executing this code prints ubl, where u is the character in the fourth position of the string trouble, and ubl are the three characters in sequence beginning in this fourth position.
In this textbox demo, startPos becomes the start position and nCharacters - startPos becomes the sequence range of characters. Here is the code again, this time extracting and printing each line of text.
You may never need to extract single lines of text in a multiline textbox, but at least you know you can.