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 ar开发者_如何学运维ray 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
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);
}
}
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
精彩评论