Private Sub Command1_Click() Dim strTmp As String, strChar As String Dim ii As Integer, RowCnt As Integer
strTmp = Text1.Text For ii = 1 To Len(strTmp) strChar = Mid(strTmp, ii, 1) If strChar = Chr$(13) Or strChar = Chr$(10) Then'换行标志 RowCnt = RowCnt + 1 End If Next ii End Sub
2楼的方法不能识别软回车,'add a textbox with "multiline=true","scrollbars=2".Private Declare Function SendMessageByNum Lib "USER32" _ Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function SendMessageByString Lib "USER32" Alias _ "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _ As Long, ByVal lParam As String) As LongPrivate Const EM_LINEINDEX = &HBB Private Const EM_GETLINE = &HC4 Private Const EM_LINELENGTH = &HC1Function GetLineText(ByVal txtbox As TextBox, ByVal LineIndex As Long) As String '返回指定行的内容 Dim lc As Long, linechar As Long linechar = SendMessageByNum(txtbox.hWnd, EM_LINEINDEX, LineIndex, 0) lc = SendMessageByNum(txtbox.hWnd, EM_LINELENGTH, linechar, 0) + 1 GetLineText = String(lc + 2, 0) Mid(GetLineText, 1, 1) = Chr(lc And &HFF) Mid(GetLineText, 2, 1) = Chr(lc \ &H100) lc = SendMessageByString(txtbox.hWnd, EM_GETLINE, LineIndex, GetLineText) GetLineText = Left(GetLineText, lc) End Function
Private Sub Command1_Click()
Dim strTmp As String, strChar As String
Dim ii As Integer, RowCnt As Integer
strTmp = Text1.Text
For ii = 1 To Len(strTmp)
strChar = Mid(strTmp, ii, 1)
If strChar = Chr$(13) Or strChar = Chr$(10) Then'换行标志
RowCnt = RowCnt + 1
End If
Next ii
End Sub
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageByString Lib "USER32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
As Long, ByVal lParam As String) As LongPrivate Const EM_LINEINDEX = &HBB
Private Const EM_GETLINE = &HC4
Private Const EM_LINELENGTH = &HC1Function GetLineText(ByVal txtbox As TextBox, ByVal LineIndex As Long) As String '返回指定行的内容
Dim lc As Long, linechar As Long
linechar = SendMessageByNum(txtbox.hWnd, EM_LINEINDEX, LineIndex, 0)
lc = SendMessageByNum(txtbox.hWnd, EM_LINELENGTH, linechar, 0) + 1
GetLineText = String(lc + 2, 0)
Mid(GetLineText, 1, 1) = Chr(lc And &HFF)
Mid(GetLineText, 2, 1) = Chr(lc \ &H100)
lc = SendMessageByString(txtbox.hWnd, EM_GETLINE, LineIndex, GetLineText)
GetLineText = Left(GetLineText, lc)
End Function