0

I got this project im working on, where when I save something to the clipboard it stores it in a textbox and then waits until I save something else and then stores that value in the textbox aswell..

Its working perfectly but I want to make it even better, because I currently have a timer as a loop that checks if the current value has been used or not (see my code below)

And I wanted to change it to where I can just compare a variable with a string, but I dont really know how to.

I tried this.. But I didnt know what to compare it too SO I couldnt move any further. var containData = Clipboard.GetText();

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)
        {
        }



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


        private void monitorButton_Click(object sender, EventArgs e)
        {
            clipboardUpdater.Enabled = true;
        }

        private void stopMonitorButton_Click(object sender, EventArgs e)
        {
            clipboardUpdater.Enabled = false;
        }
        #endregion
    }
}
Jess Chan
  • 391
  • 2
  • 14
  • 1
    Contains is case sensitive, if this is correct for your logic then the only thing that you can improve is removing the two calls to GetText. Just call Clipboard.GetText and assign the result to a variable and then use the variable for the check and for adding – Steve Jun 19 '16 at 19:16

3 Answers3

0

Given you actual code I don't think that you want a string by string comparison, but you want to have unique strings in your textbox. If this is the case then you could think to another way to store the strings instead of just a TextBox and verify everytime if the TextBox contains a string that you have already added to the control text.

There is a class called HashSet<T> that according to MSDN provides

The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

So instead of checking if the string passed via Clipboard is present in the textbox, you could check if it is present in the HashSet<string> instance

public partial class mainFrm : Form
{
    private HashSet<string> uniqueTexts = new HashSet<string>();
    public mainFrm()
    {
        InitializeComponent();
    }

    .....

    private void clipboardUpdater_Tick(object sender, EventArgs e)
    {
        string clipText = Clipboard.GetText();
        if (!uniqueTexts.Any(x => x.Equals(clipText, StringComparison.OrdinalIgnoreCase)))
        {
            clipboardSaveTextBox.AppendText("\n" + clipText);
            uniqueTexts.Add(clipText);
        }
    }
Steve
  • 213,761
  • 22
  • 232
  • 286
0

If you want to use a comparison that is not case sensitive you can just add ToLower at the both strings before the Contains method is called:

 if (!clipboardSaveTextBox.Text.ToLower().Contains(Clipboard.GetText().ToLower()))
            {
                clipboardSaveTextBox.Text += "\n" + Clipboard.GetText();
            }
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • You could store it in a variable, but I don't think it's a bottleneck of performance here. Also other methods of comparison could be implemented with a version of ToLower not to be case sensitive – meJustAndrew Jun 19 '16 at 20:40
  • As I can see, this code is inside an event, which often is called at great intervals of time, so you should not experience any performance problems at all. You don't have to worry about calling ToLower twice. – meJustAndrew Jun 19 '16 at 20:42
  • The thing is, I need it to loop through, lets say I copy something else, it should detect that, but how do I do it? I tried using a timer tick event with an interval of 0.5 seconds, but that made it so it copy pastes the same thing over and over until I copy something else then it copy pastes that over and over, get what I mean? – Jess Chan Jun 19 '16 at 21:01
  • No, what do you mean, what do youu want to copy, and what sould you detect? – meJustAndrew Jun 19 '16 at 21:09
0

What worked for me was this..

I created a timer, Is et the tick interval to 0.5 seconds, so it can repeat the monitor process every 0.5 seconds, I could put it to 0.1 seconds but I dont want to make it to heavy.

And then I added this to the method.

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

Which is.. If the clipboardSaveTextBox.Text does NOT contain the value of the clipboard it will (+= add) the current value stored in the clipboard to the clipboardSaveTextBox.Text.

I hope my answer will help someone else who might ever have the same issue.

Jess Chan
  • 391
  • 2
  • 14