Function TextBoxLineCount(txt As TextBox) As Long
Dim S As String, pos As Long
TextBoxLineCount = 0
pos = 1
S = txt.Text
Do
TextBoxLineCount = TextBoxLineCount + 1
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2 '这里为什么要加2 而不是加1?
Loop Until pos = 0
End FunctionFunction TextBoxGetLine(txt As TextBox, ByVal N As Long) As Variant
Dim S As String, pos As Long, pos2 As Long, LineIndex As Long
TextBoxGetLine = Null
LineIndex = 0
pos = 1
S = txt.Text
Do
If LineIndex = N Then
pos2 = InStr(pos, S, vbCrLf)
If pos2 > 0 Then
TextBoxGetLine = Mid(S, pos, pos2 - pos)
Else
TextBoxGetLine = Mid(S, pos)
End If
Else
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2
End If
LineIndex = LineIndex + 1
Loop Until pos = 0
End FunctionPrivate Sub Command1_Click()
MsgBox "行数=" & TextBoxLineCount(Text1)
End SubPrivate Sub Command2_Click()
MsgBox TextBoxGetLine(Text1, 21), , "第21行"
End Sub
Dim S As String, pos As Long
TextBoxLineCount = 0
pos = 1
S = txt.Text
Do
TextBoxLineCount = TextBoxLineCount + 1
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2 '这里为什么要加2 而不是加1?
Loop Until pos = 0
End FunctionFunction TextBoxGetLine(txt As TextBox, ByVal N As Long) As Variant
Dim S As String, pos As Long, pos2 As Long, LineIndex As Long
TextBoxGetLine = Null
LineIndex = 0
pos = 1
S = txt.Text
Do
If LineIndex = N Then
pos2 = InStr(pos, S, vbCrLf)
If pos2 > 0 Then
TextBoxGetLine = Mid(S, pos, pos2 - pos)
Else
TextBoxGetLine = Mid(S, pos)
End If
Else
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2
End If
LineIndex = LineIndex + 1
Loop Until pos = 0
End FunctionPrivate Sub Command1_Click()
MsgBox "行数=" & TextBoxLineCount(Text1)
End SubPrivate Sub Command2_Click()
MsgBox TextBoxGetLine(Text1, 21), , "第21行"
End Sub
vbCrLf= chr(13) + chr(10) 占两个字符所以加 2
我也觉得是这样的.