请问如何在richtextbox中选中光标所在的段落,段落分隔符为回车。
我尝试过用sendkeys实现,但效果不理想

解决方案 »

  1.   

    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
      

  2.   

    十分感谢,问题解决了
    If pos < 1 Then pos = 1
    这句还有点疑问,为何要这么做?