使用SendMessage函数向文本框发送一个 EM_GETLINECOUNT消息即可.如: Dim a as long a=sendmessage(text1.hwnd,EM_GETLINECOUNT,0,0)
不知有无现成的,自己可以写一个: Function getlinescount(txtpath As String) As Long Dim filetxt As String, x As Variant, i As Integer filetxt = String(FileLen(txtpath), " ") Open txtpath For Binary As 1 Get #1, , filetxt Close 1 x = Split(filetxt, vbCrLf) getlinescount = UBound(x) + 1 Set x = Nothing End FunctionPrivate Sub Form_Click() MsgBox getlinescount("c:\windows\win.ini") End Sub
用二进制读取方式速度较快!^_^Private Function GetTextFileLineNum(ByVal strFile As String) As Long Dim lFN As Long Dim chrTmp1 As Byte, chrTmp2 As Byte Dim lLC As Long
lFN = FreeFile lLC = 0 Open strFile For Binary As #lFN Do Until EOF(lFN) Get #lFN, , chrTmp2 If chrTmp1 = 13 And chrTmp2 = 10 Then lLC = lLC + 1 End If chrTmp1 = chrTmp2 Loop GetTextFileLineNum = lLC + 1 Close #lFN End Function
Dim a as long
a=sendmessage(text1.hwnd,EM_GETLINECOUNT,0,0)
Function getlinescount(txtpath As String) As Long
Dim filetxt As String, x As Variant, i As Integer
filetxt = String(FileLen(txtpath), " ")
Open txtpath For Binary As 1
Get #1, , filetxt
Close 1
x = Split(filetxt, vbCrLf)
getlinescount = UBound(x) + 1
Set x = Nothing
End FunctionPrivate Sub Form_Click()
MsgBox getlinescount("c:\windows\win.ini")
End Sub
Dim lFN As Long
Dim chrTmp1 As Byte, chrTmp2 As Byte
Dim lLC As Long
lFN = FreeFile
lLC = 0
Open strFile For Binary As #lFN
Do Until EOF(lFN)
Get #lFN, , chrTmp2
If chrTmp1 = 13 And chrTmp2 = 10 Then
lLC = lLC + 1
End If
chrTmp1 = chrTmp2
Loop
GetTextFileLineNum = lLC + 1
Close #lFN
End Function