Option Explicit Private Sub selectLine(rch As RichTextBox) Dim pos As Integer Dim txt As String Dim txtlen As Integer Dim crlen As Integer Dim start_pos As Integer Dim end_pos As Integer txt = rch.Text crlen = Len(vbCrLf) pos = rch.SelStart If pos < 1 Then pos = 1 start_pos = InStrRev(txt, vbCrLf, pos) If start_pos > 0 Then start_pos = start_pos + crlen - 1 end_pos = InStr(pos, txt, vbCrLf) If end_pos = 0 Then end_pos = Len(txt) rch.SelStart = start_pos rch.SelLength = end_pos - start_pos
rch.SetFocus End SubPrivate Sub Command1_Click() selectLine Me.RichTextBox1 End Sub
十分感谢,问题解决了 If pos < 1 Then pos = 1 这句还有点疑问,为何要这么做?
Private Sub selectLine(rch As RichTextBox)
Dim pos As Integer
Dim txt As String
Dim txtlen As Integer
Dim crlen As Integer
Dim start_pos As Integer
Dim end_pos As Integer
txt = rch.Text
crlen = Len(vbCrLf)
pos = rch.SelStart
If pos < 1 Then pos = 1
start_pos = InStrRev(txt, vbCrLf, pos)
If start_pos > 0 Then start_pos = start_pos + crlen - 1
end_pos = InStr(pos, txt, vbCrLf)
If end_pos = 0 Then end_pos = Len(txt) rch.SelStart = start_pos
rch.SelLength = end_pos - start_pos
rch.SetFocus
End SubPrivate Sub Command1_Click()
selectLine Me.RichTextBox1
End Sub
If pos < 1 Then pos = 1
这句还有点疑问,为何要这么做?