How can I add text to rich text box without replacing what's already there?

Joined
Apr 13, 2011
Messages
19
Programming Experience
Beginner
Not sure what I am doing wrong here but I have 2 buttons and a rich text box. The issue is when I click the second button to add text to the rich text box and it over writes the current text that is already in the rich text box. How can I code this second button so that the text is added to the rich text box and not replacing the current text?

Here's my code:

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Text = "This is the first line"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = "This is the second line"
    End Sub
End Class


Also how can I add spaces in a line of text (or in the middle of a sentence) so that I can insert a variable in the line.
Example: "This is the" variable1 "line of text"
"This is the" variable2 "line of text"
 
Last edited:
If i understood correctly then you are trying to append something onto the current data within the richtextbox?

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Text = "This is the first line"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.Text = RichTextBox1.Text & "This is the second line"
    End Sub
End Class

(thats just one way of doing it, there might be some append method , im not sure without looking).


In regards to replacing text at a certain point, you could probably search for the location of something specific and get the index? Or build the string and replace the entire contents of the richtextbox with that string when needed?

Or to simply build a string with a variable, something like

VB.NET:
dim myString as string
dim anotherString as string = "sat"

myString = "the cat " & anotherString & " on the mat"
 
Hi their depends how you want to do this but here's the easiest way.

Option Strict On 'these two lines are usually best as you wont run in to some basic errors
Option Explicit On 'also for beginner/Professional programmers i would say it's a rule of thumb.

Public Class frmMain 'Main form starting

Dim strFirst As String = "This is the first line" ' first line of text
Dim strSecond As String = "This is the second line" ' Second line of text
Dim strSpace As String = Chr(32) 'Adds a space to the end of lines so they are understandable

'Each button can be pressed in any order as they will append each other
'i hope this helps :)

Private Sub btnAdd1_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd1.Click
'Sends fist line of text to the TxtDisplay box and adds space at the end
txtDisplay.AppendText(strFirst & strSpace)
End Sub

Private Sub btnAdd2_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd2.Click
'Sends second line of text to the TxtDisplay box and adds space at the end
txtDisplay.AppendText(strSecond & strSpace)
End Sub

End Class 'End of main form

Enjoy!
 
Very simple:

RichTextBox1.Text &= "This is the second line"

This will add the second string onto the first. You may want to add a new line between the two strings:

RichTextBox1.Text &= vbCrLf & "This is the second line"
 
RichTextBox1.Text &= vbCrLf & "This is the second line"
The problem with that opposed to AppendText method is that you lose all previous formatting in the RichTextBox. The reason is that operation (a) retrieves all text as plain, (b) add with the new plain text, then (c) assigns the whole new plain text to the RichTextBox. AppendText method appends text using the last text formatting set.
 
Back
Top