How to read a word that is clicked from a RichTextBox

I have a Richtextbox that uses black and blue font color.  The blue ones are key words that I want the user to be able to click on to display a definition.  How can I read only the words that are blue to the left and/or right of the mouse click to look up my definition?
GarySBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FER_GCommented:
take a look into this example (code snippet)
Open a new project; Add a richtextBox control (rtb1) to the form, and then run the project...

FerG
Saludos.
Ing. Fernando D. Giletta
San Francisco. Córdoba. Argentina.

Private Sub Form_Load()
    With rtb1
        .Text = "I have a Richtextbox that uses black and blue font color.  The blue ones are key words that I want the user to be able to click on to display a definition.  How can I read only the words that are blue to the left and/or right of the mouse click to look up my definition?"
        .SelStart = 8
        .SelLength = 12
        .SelColor = vbBlue
        
        .SelStart = 25
        .SelLength = 5
        .SelColor = vbBlue
    End With
End Sub
 
Private Sub rtb1_Click()
    With rtb1
        If (.SelColor = vbBlue) Then
            MsgBox "show definition"
        End If
    End With
End Sub

Open in new window

0
GarySBAuthor Commented:
Almost working.  Just to let you know which VB I am using my required equivalent is: SelectionColor = Color.Blue with VB Express 2008.  What you wrote works, but I need to make the identification come into play in order to lookup proper definition .  You highlighted the words Richtextbox & uses.  When the msgbox displays I need to fill it in with DefinitionA if 'Richtextbox' was clicked or DefinitionB if 'uses' was clicked.  How can I tell which word the user clicked?  Thank You
0
FER_GCommented:
ok GarySB; here you get the code that you need. Just replace the previous code (that i put as code snippet) with the next code snippet. (replace the same code that not work previously, because the code is programed to vb6)

Now, the message will tell you what word is the word that you clicked, so you can show the definition of that word.

FerG
Saludos.
Ing. Fernando D. Giletta
San Francisco. Córdoba. Argentina.
Function WordUnderMouse(richtext As RichTextBox) As String
Dim pos    As Long
Dim i      As Integer
Dim lstart As Integer
Dim lend   As Integer
Dim llen   As Integer
Dim stxt   As String
Dim schr   As String
 
 With richtext
   llen = Len(.Text)
   stxt = .Text
   pos = .SelStart
   
   If pos > 0 Then
     'start from the chr position of the chr
     'under the mouse and work backwords one
     'character at a time looking for " " or
     'the first character in the textbox..in
     'other words, the beginning of the word
     'under the mouse
      For i = pos To 1 Step -1
        schr = Mid(stxt, i, 1)
        If schr = " " Or schr = Chr(13) Or i = 1 Then
          'if the starting character is vbcrlf then
          'we want to chop that off
          If schr = Chr(13) Then
            lstart = (i + 2)
          Else
            lstart = i
          End If
          Exit For
        End If
      Next i
 
     'start from the chr position of the chr
     'under the mouse and work fowards one
     'character at a time looking for " "
     'or the last character of the textbox...
     'in other words, the end of the word
      For i = pos To llen
        If Mid(stxt, i, 1) = " " Or _
           Mid(stxt, i, 1) = Chr(13) Or _
           i = llen Then
              lend = i
              Exit For
        End If
      Next i
      
      'prevent errors
      If lend >= lstart Then
          WordUnderMouse = Trim$(Mid$( _
               stxt, lstart, (lend - lstart)))
      End If
   End If
   
 End With
 
End Function
 
Private Sub Form_Load()
    With rtb1
        .Text = "I have a Richtextbox that uses black and blue font color.  The blue ones are key words that I want the user to be able to click on to display a definition.  How can I read only the words that are blue to the left and/or right of the mouse click to look up my definition?"
        .SelStart = 8
        .SelLength = 12
        .SelColor = vbBlue
        
        .SelStart = 25
        .SelLength = 5
        .SelColor = vbBlue
        
        .SelStart = 0
    End With
End Sub
 
Private Sub rtb1_Click()
    With rtb1
        If (.SelColor = vbBlue) Then
            MsgBox "show definition of" & vbCrLf & WordUnderMouse(rtb1)
        End If
    End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GarySBAuthor Commented:
FANTASTIC!!! Works Great
One last related question for it to be fully functional.  I'll increase the points to 500 for you.
Can the function be modified to read 'Richtext box' assuming both words are blue.  In other words when the user clicks the mouse on a blue word it should really read all words left and rgiht of the click that are blue. Thank You
0
GarySBAuthor Commented:
Thank You!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.