Jambyte
asked on
Count Lines in a Text Box
Hi,
how can i count the # of lines in a text box?
i got this much, but it only counts the # of CR's. any help would be great!
Dim n As Long
Dim pos As Long
n = 0
pos = 0
Do
pos = InStr(pos + 1, text1, Chr$(13))
n = n + 1
Loop Until pos = 0
text2 = n
MailTo:Tablet@ix.netcom.co m
how can i count the # of lines in a text box?
i got this much, but it only counts the # of CR's. any help would be great!
Dim n As Long
Dim pos As Long
n = 0
pos = 0
Do
pos = InStr(pos + 1, text1, Chr$(13))
n = n + 1
Loop Until pos = 0
text2 = n
MailTo:Tablet@ix.netcom.co
I have an answer. Unlock the question and I'll post it.
Actually, I can provide three methods for counting lines in a text box, but all require you to use the WIN32API. Text boxes are known to Windows more generally as multiline edit controls. These controls retain information internally regarding where soft line breaks occur. A hard line break would be a CRLF sequence inserted by you. Soft line breaks are retained by the insertion of CRCRLF sequences, that do not come back to you using the .text property. However, by sending a particular text formatting message to the control, you can retrieve those formaatting characters fro your own internal counting purposes. Also, (and easir) there is an api call that returns a line count, and with some additional programming you can also get a visible line count. But I do not want to steal ramrom's answer, so I'll wait on him.
ASKER
he hasn't answered for a while... go ahead!
Multiline edit controls can be thought of as a single string. You set and retrieve this string using the .text property in VB. However, Windows provides a set of edit control messages that makes it possible to treat the text as a series of lines instead of a single string. One message returns the number of lines in the control.
For instance, to get the number of lines in a control, declare the following function and constant:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_GETLINECOUNT = &HBA
then retrieve the number of lines thusly from a Text1 control:
txtlines&=nlines&(text1.Hw nd) 'Windows works off control handles, so pass it
using a function like the following:
private function nlines&(hndl)
nlines&=SendMessage(hndl,E M_GETLINEC OUNT,0,0&)
end function
Following constants (plus others) are also interesting:
'tell windows to return soft carriage returns as well as hard
Public Const EM_FMTLINES = &HC8
'get number of first visible line
Public Const EM_GETFIRSTVISIBLELINE = &HCE
'get a line
Public Const EM_GETLINE = &HC4
...and more...
I hope I haven't fumble fingered any of this in. God luck!
For instance, to get the number of lines in a control, declare the following function and constant:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_GETLINECOUNT = &HBA
then retrieve the number of lines thusly from a Text1 control:
txtlines&=nlines&(text1.Hw
using a function like the following:
private function nlines&(hndl)
nlines&=SendMessage(hndl,E
end function
Following constants (plus others) are also interesting:
'tell windows to return soft carriage returns as well as hard
Public Const EM_FMTLINES = &HC8
'get number of first visible line
Public Const EM_GETFIRSTVISIBLELINE = &HCE
'get a line
Public Const EM_GETLINE = &HC4
...and more...
I hope I haven't fumble fingered any of this in. God luck!
Sorry, I did fumble finger it in. This is better:
Place the following API declare code into the general declarations area of a bas module:
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_GETLINECOUNT = &HBA
'Calling the API from the textbox change event:
Sub Text1_Change()
Dim lineCount&
On Local Error Resume Next
'get the number of lines in the edit control
lineCount& = SendMessageLong(Text1.hwnd , EM_GETLINECOUNT, 0&, 0&)
lblLineCount = Format$(lineCount&, "##,###")
End Sub
Place the following API declare code into the general declarations area of a bas module:
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_GETLINECOUNT = &HBA
'Calling the API from the textbox change event:
Sub Text1_Change()
Dim lineCount&
On Local Error Resume Next
'get the number of lines in the edit control
lineCount& = SendMessageLong(Text1.hwnd
lblLineCount = Format$(lineCount&, "##,###")
End Sub
ASKER
What sould i save the module as? i have tried a lot of differn't names and i allways get the same error,
"methiod or data member not found' and it highlights ".hwnd"
"methiod or data member not found' and it highlights ".hwnd"
Are you talking about Visual Basic 4.0/5.0 or MS Access ???
The Visual Basic text and list controls are subclassed off of standard Windows controls and therefore respond correctly to most of the messages for these controls. They allow to be passed by their handles but what about Access text box controls? Surely, they are not the same as in VB.
The Visual Basic text and list controls are subclassed off of standard Windows controls and therefore respond correctly to most of the messages for these controls. They allow to be passed by their handles but what about Access text box controls? Surely, they are not the same as in VB.
ASKER
Im using access 97
My mistake. Access 97 controls does not seem to have an hWnd property, therefore you can not use the SendMessage API to count lines. Please reject my answer.
My mistake. Access 97 controls does not seem to have an hWnd property, therefore you can not use the SendMessage API to count lines. Please reject my answer.
One very grubby way to do it would be to send keystrokes to the text box (SendKeys command) to step through each lines with Shift+Down Arrow. Each time, check the current selection (SelText or SelLength) - when the selection hasn't changed, you've reached the end.
I haven't tried this, but it could work. Not very elegant, though!
I haven't tried this, but it could work. Not very elegant, though!
I have tried that and it works.
ASKER
could you guys explain a little bit more on that. i got the sendkeys part, what should i put in the code that will return a value if it has found a new line?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
could you explain a little more on how i apply that code?
Create a module in your Access database. Paste the first part of my answer (from Public Function ... to End Function) into that module and save it.
Then, if you've got a text box on your form called 'Text1', for example, then somewhere in the code for your form (eg. in the code behind a button), type MsgBox CountLines(Me.Text1). This will pop up a message telling you the number of lines in your text box. As CountLines is a public function, you can call it from any form, and you can assign its result to a variable (as in the second part of my answer), or put the result in another text box on your form.
Then, if you've got a text box on your form called 'Text1', for example, then somewhere in the code for your form (eg. in the code behind a button), type MsgBox CountLines(Me.Text1). This will pop up a message telling you the number of lines in your text box. As CountLines is a public function, you can call it from any form, and you can assign its result to a variable (as in the second part of my answer), or put the result in another text box on your form.
Also - people with different resolutions will also get different results.
How do you propose to distinguish a 'line'. If you had to guess you could determine the average number of characters that fit on a line - get the length of the text box using the LEN function and divide LEN/averagecharactersperli
Hope this helps
CEkman