Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSee which word in a RichTextBox the user clicked
KeywordsRichTextBox, scroll, line, print, preview, click
CategoriesControls
 
In the control's Click event handler, use the SelStart property to see where the user clicked.
 
Private Sub RichTextBox1_Click()
Dim pos1 As Integer
Dim pos2 As Integer
Dim txt As String
Dim txtlen As Integer

    ' Find the beginning of the word.
    pos1 = RichTextBox1.SelStart
    txt = RichTextBox1.Text
    If pos1 > 1 Then
        Do While Mid$(txt, pos1, 1) <> " "
            pos1 = pos1 - 1
            If pos1 <= 1 Then Exit Do
        Loop
        If pos1 > 1 Then pos1 = pos1 + 1
    End If
    
    ' Find the end of the word.
    pos2 = pos1
    txtlen = Len(txt)
    Do While Mid$(txt, pos2, 1) <> " "
        pos2 = pos2 + 1
        If pos2 > txtlen Then Exit Do
    Loop
    If pos2 <= txtlen Then pos2 = pos2 - 1
    
    txt = Mid$(txt, pos1, pos2 - pos1 + 1)
    MsgBox txt
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated