Ferds
asked on
Amount of used lines in a textbox
I need to count the amount of lines with text in a textbox on a button click. I know how to use API to count the total amount of lines and also to count the lines up to the mouse cursor.
Thanks
Thanks
ASKER
I have textbox that displays different reading passages for a reading program. I have an editor where parents and teachers can add their own content. The textbox has got a specific size depending on the level. The passages varies in length. I need to count the amount of used lines in the textbox that is used. Say if the total amount of lines is 22 and only 18 is used by text. I did get it right to count the total lines with API. I can also count up to as in example to the eightienth line if I click with the mouse on the the eightienth line. How can I count the used lines without first clicking on the last used line.
Hope this is understandeble, Thanks
Hope this is understandeble, Thanks
one way to count usedlines:
Dim txt As String
Dim usedlines As Integer
Open "test.txt" For Output As #1
Print #1, Text1
Close #1
Open "test.txt" For Input As #1
While Not EOF(1)
Line Input #1, txt
If txt > "" Then
usedlines = usedlines + 1
End If
Wend
Close #1
Kill "test.txt"
Debug.Print usedlines
Dim txt As String
Dim usedlines As Integer
Open "test.txt" For Output As #1
Print #1, Text1
Close #1
Open "test.txt" For Input As #1
While Not EOF(1)
Line Input #1, txt
If txt > "" Then
usedlines = usedlines + 1
End If
Wend
Close #1
Kill "test.txt"
Debug.Print usedlines
To count the lines that are empty in ya textbox you can use this code..
Text = vbCrLf & textbox.Text & vbCrLf
Number_of_empty_lines = CountChar(Text, vbCrLf & vbCrLf)
Function CountChar(Text As String, Char As String) As Long
Dim NrTecken As Long
CountChar = 0
NrTecken = InStr(Text, Char)
Do Until (NrTecken = 0)
CountChar = CountChar + 1
NrTecken = InStr(NrTecken + 1, Text, Char)
Loop
End Function
And ya already know how to get the linecount using API's so the rest is a piece of cake (smaller cake than this one..).
Text = vbCrLf & textbox.Text & vbCrLf
Number_of_empty_lines = CountChar(Text, vbCrLf & vbCrLf)
Function CountChar(Text As String, Char As String) As Long
Dim NrTecken As Long
CountChar = 0
NrTecken = InStr(Text, Char)
Do Until (NrTecken = 0)
CountChar = CountChar + 1
NrTecken = InStr(NrTecken + 1, Text, Char)
Loop
End Function
And ya already know how to get the linecount using API's so the rest is a piece of cake (smaller cake than this one..).
Public Function LineText(txt as textbox, lLine As Long) As String
On Error GoTo LineText_Error
Dim lRes As Long
Dim szLine As String
szLine = Space$(2000)
lRes = SendMessage(txt.hwnd, EM_GETLINE, lLine - 1, szLine)
If lRes > 0 Then
LineText = Left$(szLine, lRes)
End If
end function
public function GetUsedLines() as long
dim lLines as long
dim lI as long
dim lCt as long
dim szLine as string
lCt = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, 0&)
for lI = 1 to lCt
szLine = LineText(txt, lI)
lLines = lLines - (len(szLine) > 0)
next li
GetUsedLines = lLines
end function
On Error GoTo LineText_Error
Dim lRes As Long
Dim szLine As String
szLine = Space$(2000)
lRes = SendMessage(txt.hwnd, EM_GETLINE, lLine - 1, szLine)
If lRes > 0 Then
LineText = Left$(szLine, lRes)
End If
end function
public function GetUsedLines() as long
dim lLines as long
dim lI as long
dim lCt as long
dim szLine as string
lCt = SendMessage(txt.hwnd, EM_GETLINECOUNT, 0&, 0&)
for lI = 1 to lCt
szLine = LineText(txt, lI)
lLines = lLines - (len(szLine) > 0)
next li
GetUsedLines = lLines
end function
anthonyc: what was wrong with the code I gave? since you make your code as a answer, my code must have been wrong somehow?
1) Slow
2) I didn't even notice it until you said something. I made it an answer because that is the way to do it quickly.
Anthony
2) I didn't even notice it until you said something. I made it an answer because that is the way to do it quickly.
Anthony
ASKER
Hi VbMaster
I cannot get your code to work. I do not know if I am doing something wrong. It seems like the function is returning a 0. If I put a Debug.Print NrTecken after nrTecken=InStr(Text,Char) I get a 0 in the immediate window. The value in number_of_empty_lines is also 0. The textbox i used to test has 2 lines empty with three paragraphs of text. (A total of 20 lines with 18 used)
Thanks so far
Ferds
I cannot get your code to work. I do not know if I am doing something wrong. It seems like the function is returning a 0. If I put a Debug.Print NrTecken after nrTecken=InStr(Text,Char) I get a 0 in the immediate window. The value in number_of_empty_lines is also 0. The textbox i used to test has 2 lines empty with three paragraphs of text. (A total of 20 lines with 18 used)
Thanks so far
Ferds
You must be doing something wrong since it works okay for me. ;)
Do a Debug.Print Text in the function, are you sure you send the text of the textbox to the function??
Anthonyc.. I tried your code and it was sure fast as hell.. to crash VB.
Do a Debug.Print Text in the function, are you sure you send the text of the textbox to the function??
Anthonyc.. I tried your code and it was sure fast as hell.. to crash VB.
ASKER
Hi vbMaster
I get it to return something. It returns 8 with a textbox that has two empty lines at the end and three empty lines in between the paragraphs. That is a total of 5. I need it to return 2.
Thanks
I will also see if I can get the other codes fromanthonyc and jbil to work.
I get it to return something. It returns 8 with a textbox that has two empty lines at the end and three empty lines in between the paragraphs. That is a total of 5. I need it to return 2.
Thanks
I will also see if I can get the other codes fromanthonyc and jbil to work.
If you fee like sharing your code so I see what the **** you are doing, my email is mfitcom_suger@hotmail.com. ,)
ASKER
Hi jbill
I have tried your code with a multiline TextBox. In the formload I added words that came to three lines and then I used vbcrlf to get an empty line and then after that added more words that also came to three lines. I added your code in a command buuton's click event and got 2 in the immediate window.
Thanks
Ferds
I have tried your code with a multiline TextBox. In the formload I added words that came to three lines and then I used vbcrlf to get an empty line and then after that added more words that also came to three lines. I added your code in a command buuton's click event and got 2 in the immediate window.
Thanks
Ferds
ASKER
Hi vbmaster
I did email you the form.
thanks
Ferds
I did email you the form.
thanks
Ferds
ASKER
Hi
The code crash my machine as well
Thanks Ferds
The code crash my machine as well
Thanks Ferds
ASKER
Hi
That was anthonyc's code that crash
Thanks
Ferds
That was anthonyc's code that crash
Thanks
Ferds
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Eventually got it to work. Some day I will be good at this. lol
Thanks very much. I do appreciate it.
Ferds
Eventually got it to work. Some day I will be good at this. lol
Thanks very much. I do appreciate it.
Ferds
Ahh, have no clue what you were doing that caused those wrong results but glad you finally got it working... whatever you did.
I don't understand your problem?
Sicilian