Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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?
0
GarySB
Asked:
GarySB
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now