CharFromPos

cRuLo

Member
Joined
Jun 20, 2003
Messages
10
How do I use this message in VB.NET. I have sendmessage declared and it works with other messages, just not this one.

Code:
    Public Function GetWordUnderCursor(ByVal x As Integer, ByVal y As Integer) As String
        Dim pt As PointAPI
        Dim     As Integer
        Dim ch As String
        Dim txt As String
        Dim txtlen As Integer
        Dim    _start As Integer
        Dim    _end As Integer

        pt.x = x
        pt.y = y

         position of character under cursor
            = SendMessage(rtbData.Handle, EM_CHARFROMPOS, 0&, pt)  This shyt wont     ing work!
        If     <= 0 Then
            Exit Function
        End If
        txt = rtbData.Text

         get start position of word under cursor
        For    _start =     To 1 Step -1
            ch = Mid$(txt,    _start, 1)
            If ch = Chr(32) Or ch = vbCr Or ch = vbLf Or ch = vbNewLine Then Exit For
        Next    _start
           _start =    _start + 1

         get end position of word under cursor
        txtlen = Len(txt)
        For    _end =     To txtlen
            ch = Mid$(txt,    _end, 1)
            If ch = Chr(32) Or ch = vbCr Then Exit For
        Next    _end
           _end =    _end - 1

    End Function

the x and y values are grabbed from the MouseMove function and passed over to this code. When it goes to run the " = SendMessage(rtbData.Handle, EM_CHARFROMPOS, 0&, pt)" part, I get an Invalid parameter error or something... whats up? Everything is declared. This is how you did it in VB6...exactly how.

For some reason all the "p o s" (without the spaces) were replaced with three whitespaces.
 
Last edited by a moderator:
You do know the .NET richtextbox has a GetCharFromPosition method, right?
 
when ever i tried GetCharFromPosition it always returned the first item in the richtextbox , so heres a way i put together which may help.
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer
        x = RichTextBox1.SelectionStart() /// where the cursor is in the rtf boxs text
        RichTextBox1.SelectionLength = 1 /// get the character at that position by making selectionlength 1.
        MessageBox.Show(RichTextBox1.SelectedText)
    End Sub
 
here ya go , not sure if this is what you want , but when you move the cursor over the richtextbox it will return the character under the cursor :
Code:
    Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
        Me.Text = GetWordUnderCursor(e.X, e.Y) /// get the current letter by cursor position.
    End Sub

    Public Function GetWordUnderCursor(ByVal X As Integer, ByVal Y As Integer) As String
        Dim point As System.Drawing.Point = New System.Drawing.Point()
        point.X = X
        point.Y = Y
        Dim c As Char = RichTextBox1.GetCharFromPosition(point)
        Return c
    End Function
 
i have put together yet another way of getting the char at the position the cursor stopped at in the richtextbox :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With RichTextBox1
            .ScrollToCaret()/// get the cursor ( selection start )
            .SelectionLength = 1/// get the char ( s ) you want, 1 retrieves the first char.
            MessageBox.Show(.SelectedText)
        End With
    End Sub
 
Back
Top