2

I have made use of the _KeyDown function in my windows form:

if ((e.KeyCode == Keys.V) && (e.Modifiers == Keys.Control))
{
    // do stuff
}

and then have used to get the copied data:

 string[] clipboardRows = Clipboard.GetText(TextDataFormat.UnicodeText).Split(new string[] {"\r\n"},
                                                                                                 StringSplitOptions.None);

This works fine, however when you select say selected cells from a spreadsheet it ends up copying all cells in between for example:

1.Test
2.Test2
3.Test3
4.Test4

If i select both test and test 4 using ctrl and then copying by pressing C, when pressing ctrl + v and stepping through it gets all in between so test2 and test3.

How do i resolve this?

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
Mark W
  • 85
  • 2
  • 11
  • how do you resolve "what"? what do you expect a paste to do in such scenarios? – nawfal Jul 30 '12 at 13:52
  • 1
    This is not a WinForms or C# issue. That's just how Excel copy + paste works. Multiple discrete selections only work within Excel. Try pasting the same selection even in Word and you will find that the clipboard actually contains all cells in between. – Joshua Honig Jul 30 '12 at 14:10
  • jmh_grm, I was not aware of this... slightly dissapointed. – Mark W Jul 30 '12 at 14:22

1 Answers1

1

I'll show you why you cant do it. I recorded a Macro in Excel, entered four rows of data. I selected cell A1 and A4, press Ctrl + C

Range("A1").Select
ActiveCell.FormulaR1C1 = "abc"
Range("A2").Select
ActiveCell.FormulaR1C1 = "def"
Range("A3").Select
ActiveCell.FormulaR1C1 = "hij"
Range("A4").Select
ActiveCell.FormulaR1C1 = "klm"
Range("A4,A1").Select    'It actually concatenates the cells you've selected, that info isn't in the clipboard, if I selected three cells in the column it would be Range("A4,A1,A2").Select
Range("A1").Activate
Selection.Copy  

'Range("B1").Select
'ActiveSheet.Paste   'when I paste onto new cells, only two rows are taken up

You can do this in VSTO eg Copy & Paste VSTO - Excel Power Tools, but thats another question.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321