Solved

How to read a word that is clicked from a RichTextBox

Posted on 2009-07-14
5
180 Views
Last Modified: 2012-05-07
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
Comment
Question by:GarySB
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:FER_G
ID: 24852713
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
 

Author Comment

by:GarySB
ID: 24853045
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
 
LVL 7

Accepted Solution

by:
FER_G earned 250 total points
ID: 24858022
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
 

Author Comment

by:GarySB
ID: 24858901
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
 

Author Closing Comment

by:GarySB
ID: 31603348
Thank You!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question