Solved

How to read a word that is clicked from a RichTextBox

Posted on 2009-07-14
5
176 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now