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
FerdsAsked:
Who is Participating?
 
VbmasterConnect With a Mentor Commented:
Ferds: do you have a overclocked Celeron or something, if you get errors on this code... I finally got your form to function (damn your single form is bigger than my average program *hehe*). But I get the correct value. The EmptyLine variable sais 4 when it's 4 empty lines, 3 when it's 3 etc.
0
 
SicilianCommented:
Hi Ferds

I don't understand your problem?

Sicilian
0
 
FerdsAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jbilCommented:
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
0
 
VbmasterCommented:
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..).
0
 
anthonycCommented:
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

0
 
VbmasterCommented:
anthonyc: what was wrong with the code I gave? since you make your code as a answer, my code must have been wrong somehow?
0
 
anthonycCommented:
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
0
 
FerdsAuthor Commented:
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
0
 
VbmasterCommented:
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.
0
 
FerdsAuthor Commented:
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.
0
 
VbmasterCommented:
If you fee like sharing your code so I see what the **** you are doing, my email is mfitcom_suger@hotmail.com. ,)
0
 
FerdsAuthor Commented:
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
0
 
FerdsAuthor Commented:
Hi vbmaster

I did email you the form.

thanks
Ferds
0
 
FerdsAuthor Commented:
Hi

The code crash my machine as well

Thanks Ferds
0
 
FerdsAuthor Commented:
Hi

That was anthonyc's code that crash

Thanks
Ferds
0
 
FerdsAuthor Commented:
Hi

Eventually got it to work. Some day I will be good at this. lol

Thanks very much. I do appreciate it.

Ferds
0
 
VbmasterCommented:
Ahh, have no clue what you were doing that caused those wrong results but glad you finally got it working... whatever you did.
0
All Courses

From novice to tech pro — start learning today.