Link to home
Start Free TrialLog in
Avatar of GarySB
GarySB

asked on

RichTextBox Follow-Up (Idle_Mind)

I tried the code you wrote and it keeps giving me the same exact value for coordinates no matter where I click the mouse.  pt always equals 269, so the color is always not blue.

Dim pt As Point = Calc_Pumps_RichTxtBx.PointToClient(Cursor.Position)

Entire Routine for reading multiple colored words:
 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)
        Calc_Pumps_RichTxtBx.SelectionStart = Calc_Pumps_RichTxtBx.TextLength
        Calc_Pumps_RichTxtBx.SelectionLength = 0
        Calc_Pumps_RichTxtBx.SelectionColor = clr
        Calc_Pumps_RichTxtBx.SelectedText = " " & word & " "
    End Sub

    Private Sub Calc_Pumps_RichTxtBx_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calc_Pumps_RichTxtBx.Click
        Dim pt As Point = Calc_Pumps_RichTxtBx.PointToClient(Cursor.Position)
        Dim index As Integer = Calc_Pumps_RichTxtBx.GetCharIndexFromPosition(pt)
        Calc_Pumps_RichTxtBx.SelectionStart = index
        Calc_Pumps_RichTxtBx.SelectionLength = 1
        Dim clr As Color = Calc_Pumps_RichTxtBx.SelectionColor
        If clr.Equals(Color.Blue) Then
            While Calc_Pumps_RichTxtBx.SelectionStart > 0
                Calc_Pumps_RichTxtBx.SelectionStart = Calc_Pumps_RichTxtBx.SelectionStart - 1
                Calc_Pumps_RichTxtBx.SelectionLength = Calc_Pumps_RichTxtBx.SelectionLength + 1

                If Not Calc_Pumps_RichTxtBx.SelectionColor.Equals(clr) Then
                    Calc_Pumps_RichTxtBx.SelectionStart = Calc_Pumps_RichTxtBx.SelectionStart + 1
                    Calc_Pumps_RichTxtBx.SelectionLength = Calc_Pumps_RichTxtBx.SelectionLength - 1
                    Exit While
                End If
            End While
            While Calc_Pumps_RichTxtBx.SelectionStart + Calc_Pumps_RichTxtBx.SelectionLength < Calc_Pumps_RichTxtBx.TextLength
                Calc_Pumps_RichTxtBx.SelectionLength = Calc_Pumps_RichTxtBx.SelectionLength + 1

                If Not Calc_Pumps_RichTxtBx.SelectionColor.Equals(clr) Then
                    Calc_Pumps_RichTxtBx.SelectionLength = Calc_Pumps_RichTxtBx.SelectionLength - 1
                    Exit While
                End If
            End While

            Dim BlueWords As String = Calc_Pumps_RichTxtBx.SelectedText.Trim
            Calc_Pumps_RichTxtBx.SelectionLength = 0
            Label1.Text = BlueWords
        Else
            Calc_Pumps_RichTxtBx.SelectionLength = 0
            Label1.Text = "{Not Blue}"
        End If
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GarySB
GarySB

ASKER

Thank you for responding, Its working perfect now, go figure?  Three times in a row I got the same pt value no matter where I clicked.
Uh...ok.  Glad I could "help"...  =)