Link to home
Start Free TrialLog in
Avatar of krs_rama
krs_rama

asked on

How to select a sentence from a rich text box control ?

In rich text box control i have some sentences. each sentences consist of 2 or 3 or 4 lines each. each sentence is terminated by semicolon. how can i select the complete sentense where my cursor is currently pointing.
Avatar of corvanderlinden
corvanderlinden

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const EM_LINEFROMCHAR = &HC9

'Determine the current line in a multiline TextBox control
Function GetTextBoxCurrentLine(TB As TextBox) As Long
   GetTextBoxCurrentLine = SendMessage(TB.hWnd, EM_LINEFROMCHAR, -1, ByVal 0&) + 1
End Function
try this
private sub SelectLine()
Dim i As Long
Dim j As Long
Dim k As Long

k = RichTextBox1.SelStart

If k = Len(Trim(RichTextBox1.Text)) Then Exit Sub
While j <= k
    i = j
    j = InStr(j + 1, RichTextBox1.Text, ";")
Wend
RichTextBox1.SelStart = i
RichTextBox1.SelLength = j - (i + 1)
end sub
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or another method

Private Sub Command1_Click()
    Label1.Caption = GetSentence(RichTextBox1.Text, RichTextBox1.SelStart)
End Sub

Private Function GetSentence(ByVal RTBText As String, ByVal Position As Long) As String
    On Error Resume Next
    Dim lngCurrent As Long
    Dim lngStart As Long
    Dim lngEnd As Long
    lngStart = InStrRev(RTBText, ";", Position)
    lngEnd = InStr(Position, RTBText, ";")
    GetSentence = Mid(RTBText, lngStart + 1, lngEnd - lngStart - 1)
End Function

Avatar of Mike McCracken
Listening
Avatar of krs_rama

ASKER

good and thanks for the same