Reputation: 15
I am new to C# but am working on a project at work. The task is to copy an array of values from one application to the clipboard and then to store those values in a list of objects in C#. I have created private properties for each field represented by the columns in the copied data, but i cannot figure out how to parse the data from the clipboard and store it in each property. Hopefully this makes sense. I'm having trouble even figuring out where to begin despite extensive searching. Any help would be greatly appreciated.
Upvotes: 0
Views: 1283
Reputation: 48
I used this recently in another project: http://joshclose.github.io/CsvHelper/ Although you aren't using a CSV per se, you are using delimited data and it allows you to specify the delimeter. If you convert your text into a text reader similar to this post: In C#, how can I create a TextReader object from a string (without writing to disk), it will easily transfer the data to an object of your choosing once you have mapped it
Upvotes: 0
Reputation: 39132
As suggested by Jack A. in the comments, use String.Split().
Something like...
if (Clipboard.ContainsText())
{
string str = Clipboard.GetText();
string[] values = str.Split("\t".ToCharArray());
// ... do something with "values" ...
xxx.aaa = values[0];
xxx.bbb = values[1];
xxx.ccc = values[2];
// etc...
}
else
{
MessageBox.Show("No Text in the Clipboard!");
}
You should probably check to make sure the right number of columns were present after splitting, though:
if (values.Length == 5)
{
// ... do something with "values" ...
}
Upvotes: 2