Results 1 to 9 of 9

Thread: [RESOLVED] EM_GETLINE LaVolpe-KoolSid conversation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    Resolved [RESOLVED] EM_GETLINE LaVolpe-KoolSid conversation

    Hello,
    Recently I have use the above code which was displayed by Koolsid I belive.
    I have used it before with no single problem up to 1200 lines .
    But that was then, now I have RTB up to 3000 lines.
    Reads fine until hits 2020 line and from that moment returns to the 1st line
    and continuously duplicate it to the end of calculated number of all lines.
    In this code, is there a buffer limitation ?
    Is there something I don’t know and should know?

    Code:
    
    Option Explicit 
    
    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
        
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_GETSEL = &HB0
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    Private Const EM_LINEFROMCHAR = &HC9
    Private Const EM_GETLINE = &HC4
      Dim RTBLineCount As Long, Charindex As Long
      Dim FirstChar As Long, RowLength As Long, CursorPos As Long, nLine As Long
      Dim Buffer() As Byte, LineText A
    
    ************************************
    
    Sub part
    
    RTBLineCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
    
        
        
        For i = 0 To RTBLineCount - 1
            
            ccnt = ccnt + 1
            Charindex = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, ByVal i, ByVal CLng(0))
        
            RichTextBox1.SelStart = Charindex
        
            CursorPos = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, ByVal 0&) \ 65536
            nLine = SendMessage(RichTextBox1.hwnd, EM_LINEFROMCHAR, CursorPos, ByVal 0&)
            FirstChar = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
            RowLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, FirstChar, ByVal 0&)
            ReDim Buffer(RowLength + 1)
            Buffer(0) = RowLength + 1
    
            SendMessage RichTextBox1.hwnd, EM_GETLINE, nLine, Buffer(0)
            LineText = Left$(StrConv(Buffer, vbUnicode), RowLength-6) ' single line, less 6 right cut.
            
             
            ' ***************
           'LineText = Replace(LineText, "  ", " ") good but not to be use here
          
    	TX = Trim(LineText)
                 Do While InStr(TX, "  ")
            TX = Replace$(TX, "  ", " ") 
                 Loop
                  
           RichTextBox3.Text = RichTextBox3.Text & TX & vbNewLine
           
           Next i

  2. #2

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: EM_GETLINE LaVolpe-KoolSid conversation

    Maybe... The problem is with the EM_GETSEL call.
    If the character position at that point is > 65535 then the return value is -1 and -1\ 65536 is zero, back to the first character. There is a workaround

    But first, I'm confused by your code:
    1. You are looping thru RTF#1, ok
    2. You are setting CharIndex to 1st character of each line, ok
    3. You are setting the .SelStart, why? Just for visual effect?
    4. You are then trying to set CursorPos which is failing but CursorPos should equal CharIndex when it doesn't fail
    5. You are trying to set nLine, but isn't nLine = i ?
    6. You are setting FirstChar, but FirstChar should = CharIndex
    7. RowLength at this point could be retrieved by using CharIndex

    So from what I see is CharIndex = FirstChar = CursorPos (if -1 not returned). Also i = nLine

    Aside from the confusion, if you need the workaround to get a valid character index from a rtf that has more than 65K characters, I can provide that.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    Re: EM_GETLINE LaVolpe-KoolSid conversation

    Yes, I would like to leave it as is as a new post.
    I have found a conversation between those two guys in a similar topic
    and I was hoping that one of them, or anyone in this matter would explain it to me.
    Many thanks,

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    Re: EM_GETLINE LaVolpe-KoolSid conversation

    LaVolpe, Thanks for replay.
    Any idea from you would be greatly appreciated to correct all this
    and have better understanding the issue here.
    Much appreciated.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: EM_GETLINE LaVolpe-KoolSid conversation

    If you want to try to debug you could set a breakpoint to pause the code when i gets to 2020 and then step through the code line by line examining the variables to see what's happening. That's a pretty sure way to find the problem and a solution.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: EM_GETLINE LaVolpe-KoolSid conversation

    A quick fix, using your code.... but tweaking it
    Code:
    For nLine = 0 To RTBLineCount - 1
            
            ccnt = ccnt + 1
            Charindex = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
        
            RichTextBox1.SelStart = Charindex
            RowLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, CharIndex, ByVal 0&)
            ReDim Buffer(RowLength + 1)
            Buffer(0) = RowLength + 1
    
            SendMessage RichTextBox1.hwnd, EM_GETLINE, nLine, Buffer(0)
            LineText = Left$(StrConv(Buffer, vbUnicode), RowLength-6) ' single line, less 6 right cut.
            
    ...
    Copy your current code before replacing this, just in case the tweaks affect something else you didn't show us.
    Note that the variables i, FirstChar, CursorPos are no longer used in the above tweaks.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Toronto, Canada
    Posts
    76

    Smile Re: EM_GETLINE LaVolpe-KoolSid conversation

    Hello LaVolpe,

    A “ quick fix “ does work OK,
    I had to change this line to:
    Code:
    For nLine = 0 To RTBLineCount - 2
    I misted the Line counter somewhere on the way, but that is nothing compare to what it was.
    I also must stick to this routine from now on since I use this procedure a lot.
    Actually, if you would have in your sleeve this procedure written your way
    I would appreciate if you could share this idea with me.
    Many Thanks, problem solved, excellent respond!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width