How to read word(s) from RichTextBox only if blue

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 some 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.

silemoneCommented:
if you convert it to xml, it will have all of the attributes and you can then see the colors of the text...
0
GarySBAuthor Commented:
I'm already using a richtextbox for highlighting purpose.  I've seen some other code sample that do something similar, but wrapping might be a issue.  Can the letter/character position of the click be found? and How to test the color of the letter for forcolor blue?
0
GarySBAuthor Commented:
How do you even add a xml? to a Panel or Form?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GarySBAuthor Commented:
I prefer a Richtextbox solution
0
Bob LearnedCommented:
I would think that you could start with the SelectionStart, move the selection back until you find a non-blue character, and then move forward until you get the next non-blue character, or the end of the text.  Then, you would have you range, and you could get the text between those 2 character index markers.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Using Bob's suggestion, here is a slight variation:
(my test project had just a Label and a RichTextBox)
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim words() As String = {"cat", "dog", "fish", "hamster", "snake"}
        Dim colors() As Color = {Color.Red, Color.Blue, Color.Green}
        Dim R As New Random
        For i As Integer = 1 To 50
            AppendWord(words(R.Next(0, words.Length)), colors(R.Next(0, colors.Length)))
        Next
    End Sub
 
    Private Sub AppendWord(ByVal word As String, ByVal clr As Color)
        RichTextBox1.SelectionStart = RichTextBox1.TextLength
        RichTextBox1.SelectionLength = 0
        RichTextBox1.SelectionColor = clr
        RichTextBox1.SelectedText = " " & word & " "
    End Sub
 
    Private Sub RichTextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.Click
        Dim pt As Point = RichTextBox1.PointToClient(Cursor.Position)
        Dim index As Integer = RichTextBox1.GetCharIndexFromPosition(pt)
        RichTextBox1.SelectionStart = index
        RichTextBox1.SelectionLength = 1
        Dim clr As Color = RichTextBox1.SelectionColor
        If clr.Equals(Color.Blue) Then
            While RichTextBox1.SelectionStart > 0
                RichTextBox1.SelectionStart = RichTextBox1.SelectionStart - 1
                RichTextBox1.SelectionLength = RichTextBox1.SelectionLength + 1
 
                If Not RichTextBox1.SelectionColor.Equals(clr) Then
                    RichTextBox1.SelectionStart = RichTextBox1.SelectionStart + 1
                    RichTextBox1.SelectionLength = RichTextBox1.SelectionLength - 1
                    Exit While
                End If
            End While
            While RichTextBox1.SelectionStart + RichTextBox1.SelectionLength < RichTextBox1.TextLength
                RichTextBox1.SelectionLength = RichTextBox1.SelectionLength + 1
 
                If Not RichTextBox1.SelectionColor.Equals(clr) Then
                    RichTextBox1.SelectionLength = RichTextBox1.SelectionLength - 1
                    Exit While
                End If
            End While
 
            Dim BlueWords As String = RichTextBox1.SelectedText.Trim
            RichTextBox1.SelectionLength = 0
            Label1.Text = BlueWords
        Else
            RichTextBox1.SelectionLength = 0
            Label1.Text = "{Not Blue}"
        End If
    End Sub
 
End Class

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
Mike TomlinsonMiddle School Assistant TeacherCommented:
I didn't realize this was a duplicate question...  =\

Anyhoo, at the very bottom of the other question the author states:

    "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."

The approach I posted here is different from the other and actually handles this additional requirement.  The other solution is inspecting the characters looking for word boundaries while my approach is simply looking for where the blue portion starts and stops.

Just thought I'd throw that out there...delete the question if you want.  =)
0
GarySBAuthor Commented:
Thank you for the routine.  As you stated the other solution works for a single word.
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
.NET Programming

From novice to tech pro — start learning today.