Home All Groups Group Topic Archive Search About

EM_CHARFROMPOS with RichTextBox

Author
6 Jul 2006 3:04 AM
mike w.
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

Author
6 Jul 2006 5:34 AM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
6 Jul 2006 9:32 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"mike w." <mi***@discussions.microsoft.com> schrieb:
> 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 _
    )
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
6 Jul 2006 12:41 PM
mike w.
That worked!!!
thanks

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "mike w." <mi***@discussions.microsoft.com> schrieb:
> > 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 _
>     )
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>
Author
6 Jul 2006 2:58 PM
mike w.
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?

Show quoteHide quote
"mike w." wrote:

> That worked!!!
> thanks
>
> "Herfried K. Wagner [MVP]" wrote:
>
> > "mike w." <mi***@discussions.microsoft.com> schrieb:
> > > 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 _
> >     )
> > ///
> >
> > --
> >  M S   Herfried K. Wagner
> > M V P  <URL:http://dotnet.mvps.org/>
> >  V B   <URL:http://classicvb.org/petition/>
> >
> >
Author
7 Jul 2006 3:24 PM
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

Show quoteHide quote
"mike w." <mi***@discussions.microsoft.com> wrote in message
news:97CD48A1-3301-43E8-AA6F-46AD85C64881@microsoft.com...
> 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?
>
> "mike w." wrote:
>
>> That worked!!!
>> thanks
>>
>> "Herfried K. Wagner [MVP]" wrote:
>>
>> > "mike w." <mi***@discussions.microsoft.com> schrieb:
>> > > 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 _
>> >     )
>> > ///
>> >
>> > --
>> >  M S   Herfried K. Wagner
>> > M V P  <URL:http://dotnet.mvps.org/>
>> >  V B   <URL:http://classicvb.org/petition/>
>> >
>> >
Author
7 Jul 2006 5:27 PM
mike w.
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

Show quoteHide quote
"Claes Bergefall" wrote:

> 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
>
> "mike w." <mi***@discussions.microsoft.com> wrote in message
> news:97CD48A1-3301-43E8-AA6F-46AD85C64881@microsoft.com...
> > 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?
> >
> > "mike w." wrote:
> >
> >> That worked!!!
> >> thanks
> >>
> >> "Herfried K. Wagner [MVP]" wrote:
> >>
> >> > "mike w." <mi***@discussions.microsoft.com> schrieb:
> >> > > 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 _
> >> >     )
> >> > ///
> >> >
> >> > --
> >> >  M S   Herfried K. Wagner
> >> > M V P  <URL:http://dotnet.mvps.org/>
> >> >  V B   <URL:http://classicvb.org/petition/>
> >> >
> >> >
>
>
>