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
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