Solved

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

Posted on 2009-07-13
11
213 Views
Last Modified: 2013-11-08
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?
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
11 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 24842877
if you convert it to xml, it will have all of the attributes and you can then see the colors of the text...
0
 

Author Comment

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

Author Comment

by:GarySB
ID: 24842948
How do you even add a xml? to a Panel or Form?
0
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!

 

Author Comment

by:GarySB
ID: 24843298
I prefer a Richtextbox solution
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24876681
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 24888224
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24896650
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
 

Author Closing Comment

by:GarySB
ID: 31602966
Thank you for the routine.  As you stated the other solution works for a single word.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

749 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