1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Visual Basic .NET' started by simpleonline1234, Sep 15, 2011.

  1. simpleonline1234

    simpleonline1234 Junior Member

    Joined:
    Jan 26, 2010
    Messages:
    169
    Likes Received:
    13
    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:

    Code:
    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"
     
  2. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    Here is a quick example. You should be able to figure this out without comments.

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.Text = "This is a Test"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RichTextBox1.AppendText(Environment.NewLine & "This is more text")
        End Sub