- This topic has 3 replies, 3 voices, and was last updated 11 years ago by Chike.
-
AuthorPosts
-
February 7, 2013 at 10:41 pm #186668pharaonMember
i have this function to get the last line from pal room..it work fine under compile target net framework 3.5 . but when i compile the app for target framework 4
some times it return value as text and other times it dosn’t return text at all…any explnation or how to make it work for net framework 4Private Function GetLastLineTextChat(ByVal hwnd As IntPtr, ByRef iLastLine As Integer) As String() Dim lngCount As Integer Dim lngLength As Integer Dim strBuffer As New StringBuilder(255) Dim sText() As String = Nothing 'Get Line count lngCount = SendMessage(hwnd, EM_GETLINECOUNT, 0, 0) lngLength = SendMessage(hwnd, EM_LINELENGTH, lngCount - 2, 0) 'get line text If iLastLine = 0 Then iLastLine = lngCount If lngCount > iLastLine Then Do While iLastLine <= lngCount iLastLine = iLastLine + 1 If sText Is Nothing Then ReDim sText(0) Else Dim iCount As Integer = sText.Length ReDim Preserve sText(iCount) End If 'get line text Call SendMessageString(hwnd, EM_GETLINE, iLastLine - 2, strBuffer) sText(sText.Length - 1) = strBuffer.ToString If iLastLine = lngCount Then Exit Do End If Loop ElseIf iLastLine <> lngCount Then iLastLine = lngCount ReDim sText(0) 'get line text Call SendMessageString(hwnd, EM_GETLINE, iLastLine - 2, strBuffer) sText(sText.Length - 1) = strBuffer.ToString ElseIf iLastLine = lngCount Then End If Return sText End Function
February 8, 2013 at 12:01 am #186671ChikeMemberWhat does this mean
sText(sText.Length - 1) = strBuffer.ToString
and this
Dim iCount As Integer = sText.Length
ReDim Preserve sText(iCount)
Use a string builder to collect all lines in the loop, it’s surely more efficient, readable, and tou won’t need to ask “why”.
And after the elseif a sipmle
sText = strBuffer.ToString
would do.
February 8, 2013 at 1:51 pm #186670autopilotMemberThis code was part an example I posted here a long time ago.
@Chike wrote:
And after the elseif a sipmle
sText = strBuffer.ToString
would do.
If you are just putting all lines into a single text object, that may work. However, the code he has provided adds each line to the end of a string array. So for each line, it increases the size of the array by 1 and then adds the line to the end of the array.
Therefore, to answer:
@Chike wrote:What does this mean
sText(sText.Length - 1) = strBuffer.ToString
It means to load the last entry of the string array with the current content of the stringbuilder.
and this:
@Chike wrote:Dim iCount As Integer = sText.Length
ReDim Preserve sText(iCount)“ReDim” re-sizes the string array
“Preserve” saves the current content.It could be condensed into a single line like this:
ReDim Preserve sText(sText.Length)
It is possible, I guess, to get all the lines in a single StringBuilder and then use .Split to add them into the String Array, but I have never approached it that way.
As to why you are having problems with .NET4, I have no ideas. The code is all compatable with .NET4 and I dont see anything in it that should cause what you are reporting. You may want to add some Try/Catch statements to see what errors (if any) are being reported back.
February 8, 2013 at 5:56 pm #186669ChikeMemberAh it’s stupid line array.
still would make sense to
Redim sText(lngCount - iLastLine )
once, and
[code)
sText(0) = strBuffer.ToString
[/code]So the problem must be the first word of the string buffer that should be set to the capacity before sending the EM_GETLINE message
strBuffer.Chars(0) = Chr(255)
strBuffer.Chars(1) = Chr(0)
If the project is set to ansi, or just the first line if using unicode
-
AuthorPosts
Related
- You must be logged in to reply to this topic.