Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding texbox value to end of rich textbox

Status
Not open for further replies.

CupraSA

Technical User
Mar 14, 2009
10
ZA
Hi,

I am relatively new to VB6 and have the following problem:

I need to create a "audit trail" of what has happed in the form and display it in a rich textbox. So basically I want to add the value of textbox1 to the end of the existing contents in richtextbox1 when I save the form.

How do I add text to the rich textbox without clearing the existing info?

Thanks
 
To avoid expensive copying (interrogating and setting rtb.Text) and concatenation overhead when such logs get large I try to follow a pattern like:
Code:
Option Explicit

Private Const EM_SETSEL = &HB1

Private Declare Function SendMessageLng Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Private Sub Log(Optional ByVal Text As String = "")
    With RichTextBox1
        SendMessageLng .hWnd, EM_SETSEL, -1, 0 'Set insertion point at end.
        .SelText = Text & vbNewLine
        SendMessageLng .hWnd, EM_SETSEL, -1, 0
    End With
End Sub

Private Sub Form_Load()
    Dim I As Integer
    
    Show
    
    For I = 1 To 10
        Log "Line " & CStr(I)
    Next I
End Sub
 
Perfect! Thanks a lot guys!

Dilettante, Just a question why does it add the text for "Log" above the existing contents?
 
Above? It adds at the end (below) here.

The steps used should set the insertion point at the end, insert new text there (the end), and then reset the insertion point at the (new) end so the RTB is scrolled to show the last part of the log.


I missed the part about "saving the form." That would need to be handled by writing the RTB contents out to disk every Form_Unload and reading it back each Form_Load.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top