Solved

How to read a word that is clicked from a RichTextBox

Posted on 2009-07-14
5
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 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