kevin_lam Posted March 9, 2005 Posted March 9, 2005 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 Quote
Tygur Posted March 9, 2005 Posted March 9, 2005 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. Quote
coldfusion244 Posted March 9, 2005 Posted March 9, 2005 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. Quote -Sean
Recommended Posts