views:

715

answers:

1

Hi, I have a form with a richtextbox and a scrollbar. I want to be able to keep appending text to the textbox without loosing my selection. I can save the selectionstart and selectionlength before I append the text and set it back and it works. The problem is when it comes to reverse selecting. The best I could do is set back the selectionstart at the right position but I have to move the mouse so it selects text again. I'm using the following functions before and after I append the text:

Private Sub StopRepaint()
Dim pt As Point
' Stop redrawing:
SendMessage(txtchat.Handle, WM_SETREDRAW, 0, pt)
' Stop sending of events:
eventmask = SendMessage(txtchat.Handle, EM_GETEVENTMASK, 0, pt)
End Sub

Private Sub StartRepaint()
Dim pt As Point
' turn on events
SendMessage(txtchat.Handle, EM_SETEVENTMASK, 0, pt)
' turn on redrawing
SendMessage(txtchat.Handle, WM_SETREDRAW, 1, pt)
' this forces a repaint, which for sotxtchat reason is necessary in sotxtchat cases.
txtchat.Invalidate()
End Sub

If my scrollbar position is not at complete bottom, i'm setting it back using this after the text is appended:

If Not isbottom(getpos) Then
    PostMessageA(txtchat.Handle, WM_VSCROLL, SB_THUMBPOSITION + _
                       &H10000 * getpos(), Nothing)
End If

My main goal is to be able to select and copy text even when new text is being appended to the richtextbox. Anyone knows how to correctly select text in reverse OR easily fix my problem about text being deselected? Thanks.

+1  A: 

I would try making "selectable blocks" keep each appended slab of text independant of the next block. Sort of like MSN messenger. You can make the "panels" appear seamless and using a stringbuilder can export the final block safely as a single stream.

Rob