Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

 

I have been trying to use SendMessage to determine no. of characters in each line within a textbox.

 

API Declaration

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal wParam As Int32, ByVal lParam As Object) As Long

Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal wParam As Int32, ByRef lParam As String) As Long

Private Const EM_GETLINE As Int32 = &HC4

Private Const EM_GETLINECOUNT As Int32 = &HBA

Private Const EM_LINEINDEX As Int32 = &HBB

Private Const EM_LINELENGTH As Int32 = &HC1

 

Code to determine length of each line

 

Dim lngCount As Long, lngLineIndex As Long, lngLength As Long

Dim i As Int32

 

Dim strBuffer As String

'Get Line count

lngCount = SendMessage(txtContent1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)

txtCount.Text = ""

txtLineCount.Text = ""

 

With txtContent1

For i = 0 To lngCount - 1

'Get line index

lngLineIndex = SendMessage(.Handle, EM_LINEINDEX, i, Nothing)

 

'lngLineIndex ALWAYS return 0 !!

 

'get line length

lngLength = SendMessage(.Handle, EM_LINELENGTH, lngLineIndex, Nothing)

 

'lngLength ALWAYS return length of 1st line

 

'resize buffer

strBuffer = Space(lngLength)

'get line text

 

SendMessageStr(.Handle, EM_GETLINE, i, strBuffer)

''Number each line

txtLineCount.Text &= "line " & i & " : " & strBuffer.Length & ", " & lngLength & ControlChars.CrLf

Next

End With

 

Can someone help me to take a look the code and see what's wrong ?

 

Thanks :o

Posted

In your SendMessage declarations, wMsg should be declared as Int32, not Long.

 

Also, I would be declaring lParam as Int32 and passing zero, rather than declaring it as Object and passing Nothing. What you're doing is probably fine, tho, but it'd be something to try after changing wMsg to Int32 if things still don't work.

Posted
In your SendMessage declarations, wMsg should be declared as Int32, not Long.

 

Also, I would be declaring lParam as Int32 and passing zero, rather than declaring it as Object and passing Nothing. What you're doing is probably fine, tho, but it'd be something to try after changing wMsg to Int32 if things still don't work.

 

I just find it odd that it's set up for ByVal and not ByRef. If you're going to be passing an arguement to be changed I would pass it's reference, not it's value. I know C# has the 'out' modifier... Just my thoughts.

-Sean
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...