Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;


C#
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();
        }
    }
}


What I have tried:

I tried doing
richTextBox1.Text += Clipboard.GetText(); but that just adds one after the other, I need to make it only count once
Posted
Updated 18-Jun-16 19:23pm

1 solution

Instead of a timer use the Clipboard WM_CLIPBOARDUPDATE: .net - How to monitor clipboard content changes in C#? - Stack Overflow[^]

Also save the last text in a variable and recheck that and the current text from the clipboard and do your stuff if it has changed.
 
Share this answer
 
Comments
BladeLogan 19-Jun-16 1:26am    
I would have to invoke and lots of other stuff.. I went with this method..

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


Much easier
Mehdi Gholam 19-Jun-16 1:27am    
Then use the second part of my suggestion.
Sergey Alexandrovich Kryukov 19-Jun-16 1:56am    
5ed.
—SA
Mehdi Gholam 19-Jun-16 1:57am    
Cheers Sergey!
BladeLogan 19-Jun-16 1:59am    
And dont get me wrong, the other solution with the variable sounds amazing, simple and yet very good, the thing is I wouldnt know where to go after createing the variable that would store the value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900