tags:

views:

66

answers:

2

i tried to make a array list of 5 element in which recent 5 clipboard text is copied

but i am not able to do this every time clipboard text overwrites the previous one and in first array element

and prints only the last one i want to print all how can i do this.

if my case is possible thn please give me some solution

+1  A: 

How about: You manage a custom object while you read/write on Clipboard. For instance, MyCustomClipboardClass.

Everytime you are about to move data on clipboard;

  • Get your MyCustomClipboardClass object.
  • Add your text to it.
  • Save that object onto clipboard.

See following:

[Serializable]
class MyCustomClipboardClass
{
    List<string> m_lstTexts = new List<string>();

    public void AddText(string str)
    {
        m_lstTexts.Add(str);
    }
}
KMan
A: 

You can do something like that, if I understand question correctly (if you want to keep last 5 clipboard items programatically):

    const int MaxItems = 5;
    static readonly List<string> ClipboardData = new List<string>();

    public static void SaveClipboard()
    {
        ClipboardData.Add(Clipboard.GetText());
        if (ClipboardData.Count > MaxItems) ClipboardData.RemoveAt(0);
    }

    // You don't need lines later, I show them just as example
    [STAThreadAttribute]
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            Clipboard.SetText(i.ToString());
            SaveClipboard();
        }

        foreach (var s in ClipboardData)
        {
            Console.WriteLine(s);
        }

        Console.ReadLine();
    }

If you need @KMan way check this question also: C#/WPF Can I Store more that 1 type in Clipboard?


So you have to call SaveClipboard() after each clipboard modification. All data will be gathered in ClipboardData

Nick Martyshchenko
i tried this one but in both win form app and console app.
shruti
console app. shows error that clipboard doesnt found. for that i have to include using System.Windows.Forms; in console app. program
shruti
nd in win form app. it wont wrk it shows Main() error
shruti
wht i need to do please help me i am not good programmenr
shruti
Reference manually (via Right click on References -> AddReference) System.Windows.Forms, it will helps.
Nick Martyshchenko
In WinForms you don't need my Main method it just demostrates how you can try to use SaveClipboard() and list contents..
Nick Martyshchenko
hii... i add references the program is run successfully but it shows output 56789 all in new line but i expect it will show any copied data which is in clipboard :( :( :(
shruti
So, what *exactly* you want? Do you need to monitor clipboard data and gather data from any (including external) clipboard modification? Or do you need to programmatically gather data which *your own* program will add? You get 5..6..7..8.. thru I called `Clipboard.SetText()` as example and modify clipboard. Be sure to read `Clipboard` class details.
Nick Martyshchenko