SendMessage API function

L

Lee Weiner

I'm a VB6 guy playing around with VB.NET. Trying to convert one of my old VB6
projects, a plain old text editor. A problem I'm having is using the
SendMessage API function to retrieve the line in the textbox the insertion
cursor is on. I have the following declarations:

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB

In VB6, the first parameter is a Long, but in .NET, the Handle property is an
IntPtr, so I changed the declaration. I'm not sure that was the right thing to
do. I suppose there might be a way to cast an IntPtr as a Long but I couldn't
figure out how.

These two lines of code:

line = SendMessage(txtEditor.Handle, EM_LINEFROMCHAR, _
txtEditor.SelectionStart, 0)
col = SendMessage(txtEditor.Handle, EM_LINEINDEX, line, 0)

both return 0 no matter what the actual cursor position in the textbox is.
The first call should return the actual line in the textbox the cursor is on.
The second should return the index of the first character on the line
specified.

Can someone tell me what I'm doing wrong or if there's a new .NET way of
accomplishing the same task?

Lee Weiner
 
C

Cor Ligthert

Lee,

Your question has been a lot of times in this newsgroup.

The main answer is that there is a shift in the declaration of values
between VB6 and VBNet.
In VBNet an Int32 is an Integer while that was in VB6 a Long and the same
shift with the other values.

However your almost exact question has been so often in this newsgroup, that
in my opinion it is the best just looking too the answers for that problem
to get all problems with this function.

http://groups-beta.google.com/group...+"user32"&qt_g=1&searchnow=Search+this+groupI hope this helps?Cor
 
H

Herfried K. Wagner [MVP]

Lee Weiner said:
I'm a VB6 guy playing around with VB.NET. Trying to convert one of my old
VB6
projects, a plain old text editor. A problem I'm having is using the
SendMessage API function to retrieve the line in the textbox the insertion
cursor is on. I have the following declarations:

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB

In VB6, the first parameter is a Long, but in .NET, the Handle property is
an
IntPtr, so I changed the declaration. I'm not sure that was the right
thing to
do. I suppose there might be a way to cast an IntPtr as a Long but I
couldn't
figure out how.

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

Private Const EM_LINEFROMCHAR As Int32 = &HC9
Private Const EM_LINEINDEX As Int32 = &HBB
///

'Long' is a 64-bit datatype in VB.NET!
 
L

Lee Weiner

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

Private Const EM_LINEFROMCHAR As Int32 = &HC9
Private Const EM_LINEINDEX As Int32 = &HBB
///

'Long' is a 64-bit datatype in VB.NET!

Got it. Thank you.

Lee Weiner
 

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