RichTextBox Follow-Up (Idle_Mind)

GarySB
GarySB used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Not sure what you've done different as it works great for me.  Using VB.Net 2008 on a 64 bit Vista Home Premium system.

I just created a new project and pasted the code.  I added a RichTextBox and named it "Calc_Pumps_RichTxtBx" along with a Label (defaulted to "Label1" of course).

I added some Debug lines:

    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)
        Debug.Print("pt = " & pt.ToString)
        Debug.Print("index = " & index)
        Debug.Print("-------------------------------------")

        ...

Ran it...and this is what I got:
pt = {X=289,Y=12}
index = 64
-------------------------------------
pt = {X=234,Y=38}
index = 228
-------------------------------------
pt = {X=206,Y=19}
index = 136
-------------------------------------
pt = {X=375,Y=38}
index = 260
-------------------------------------

Open in new window

Author

Commented:
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.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Uh...ok.  Glad I could "help"...  =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial