EM_CHARFROMPOS with RichTextBox

G

Guest

I'm trying to get a char when a user right cliks in a RichTextBox, but I keep
geting
"An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object."
in my SendMessage call

The wierd thing is if I change the code below to handle a TextBox instead
everything works fine.
What do I need to do to make this work?

the code
Private Declare Function SendMessageLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Long

Private Sub rtbStuff_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles rtbStuff.MouseDown
If e.Button = MouseButtons.Right Then
rtbStuff.Select(RTBCursorPos(rtbStuff, e.X, e.Y), 0)
End If
End Sub

Public Function RTBCursorPos(ByVal txt As RichTextBox, ByVal X As
Single, ByVal Y As Single) As Long
' Convert screen coordinates into control coordinates.
Dim pt As Point = txt.PointToClient(New Point(X, Y))

' Get the character number
Dim lTemp As Long
lTemp = SendMessageLong(txt.Handle, EM_CHARFROMPOS, 0&, CLng(pt.X +
pt.Y * &H10000)) And &HFFFF&

RTBCursorPos = lTemp
End Function
 
M

Mattias Sjögren

What do I need to do to make this work?
the code
Private Declare Function SendMessageLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Long

For a RichTextBox the declaration should be

.... ByRef lParam As POINT) As IntPtr


Mattias
 
H

Herfried K. Wagner [MVP]

mike w. said:
I'm trying to get a char when a user right cliks in a RichTextBox, but I
keep
geting
"An unhandled exception of type 'System.NullReferenceException' occurred
in
system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object."
in my SendMessage call

The wierd thing is if I change the code below to handle a TextBox instead
everything works fine.
What do I need to do to make this work?

the code
Private Declare Function SendMessageLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32) As Long

\\\
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32
///
' Get the character number
Dim lTemp As Long
lTemp = SendMessageLong(txt.Handle, EM_CHARFROMPOS, 0&, CLng(pt.X +
pt.Y * &H10000)) And &HFFFF&

RTBCursorPos = lTemp
End Function

\\\
Dim n As Int32 = _
SendMessage( _
txt.Handle, _
EM_CHARFROMPOS, _
0, _
(pt.X + pt.Y * &H10000) And &HFFFF _
)
///
 
G

Guest

That worked!!!
thanks

Herfried K. Wagner said:
\\\
Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32
///



\\\
Dim n As Int32 = _
SendMessage( _
txt.Handle, _
EM_CHARFROMPOS, _
0, _
(pt.X + pt.Y * &H10000) And &HFFFF _
)
///
 
G

Guest

Ok, it worked in the short term
repeated clicking gives it the same error message on the SendMessage call

so now what do I do?
 
C

Claes Bergefall

As Mattias pointed out the lParam parameter should be a reference to a
POINTL structure. This is clearly stated in the docs for EM_CHARFROMPOS:

"lParam
Specifies the coordinates of a point in the control's client area. The
coordinates are in screen units and are relative to the upper-left corner of
the control's client area.
Rich edit controls: This is a pointer to a POINTL structure that contains
the horizontal and vertical coordinates.
Edit controls: The low-order word contains the horizontal coordinate. The
high-order word contains the vertical coordinate."

Private Structure POINTL
Public x As Int32
Public y As Int32
End Structure

Private Declare Auto Function SendMessage Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByRef lParam As POINTL _
) As Int32

/claes
 
G

Guest

ok,
what you gave me now makes the SendMessage call not crash
i also had to remove this line of code
RichTextBox.PointToClient(New Point(X, Y)) and use the strait X and Y that
the mouse provides from RichTextBox1_MouseDown for my POINTL values
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top