Jess Chan
Jess Chan

Reputation: 439

How do I save the first text in a textbox and not overwrite it C# .NET

I am working on a very basic project where I can copy something to the clipboard and it saves it in a RichTextBox in my application. I've made it loop through and check the clipboard every 0.5 seconds with a timer but how do I make the first copy stay in the TextBox because what it does now is:

-I copy something to the clipboard
-It sends it to the TextBox
-When I copy something else it overwrites it

How do I make them add one after the other?

This is what I got so far;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CBR
{
    public partial class mainFrm : Form
    {
        public mainFrm()
        {
            InitializeComponent();
        }

        private void mainFrm_Load(object sender, EventArgs e)
        {
        }

        private void clipboardUpdater_Tick(object sender, EventArgs e)
        {
            richTextBox1.Text = Clipboard.GetText();
        }
    }
}

Upvotes: 2

Views: 80

Answers (1)

Alex Diamond
Alex Diamond

Reputation: 586

Seems like this is what you're looking for;

    private void clipboardUpdater_Tick(object sender, EventArgs e)
    {
        if (!richTextBox1.Text.Contains(Clipboard.GetText()))
        {
            richTextBox1.Text += Clipboard.GetText();
        }
    }

If you want to seperate each paste, replace the statement with this;

richTextBox1.Text += " " + Clipboard.GetText();

Upvotes: 3

Related Questions