|
Users often want to provide Cut, Copy, and Paste functionality to their applications. To do this, use the KeyDown event to determine what combination of keys has been pressed, and then execute the Cut, Copy, or Paste code accordingly.
Option Explicit
Private Sub SGGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim myData As String
‘Check to see if CTRL is pressed
If Shift = vbCtrlMask Then
‘Check to see if “C” is pressed
If KeyCode = vbKeyC Then ' COPY
'Check to see if you are in edit mode. If you are, copy the selected text. If you are not, copy the selection range.
If SGGrid1.Editing = True Then
'Copy the selected text
myData = SGGrid1.EditSelText
'Cancel editing
SGGrid1.Editing = False
Else
'Copy the data in the selection range
myData = SGGrid1.Selection.Ranges(1).GetData(sgFormatCharSeparatedValue, sgExportDefault, "|", "*")
End If
'Clear the clipboard
Clipboard.Clear
'Copy our selected data to the clipboard
Clipboard.SetText myData
‘Check to see if “X” is pressed
ElseIf KeyCode = vbKeyX Then ' CUT
'Check to see if you are in edit mode. If you are, copy the selected text. If you are not copy the selection range.
If SGGrid1.Editing = True Then
'Copy the selected text
myData = SGGrid1.EditSelText
'Cancel editing
SGGrid1.Editing = False
Else
'Copy the data in your selection range
myData = SGGrid1.Selection.Ranges(1).GetData(sgFormatCharSeparatedValue, sgExportDefault, "|", "*")
End If
'Clear the clipboard
Clipboard.Clear
'Copy your selected data to the clipboard
Clipboard.SetText myData
'Clear the selection range
Call ClearRange(SGGrid1.Selection.Ranges(1))
‘Check to see if “P” is pressed
ElseIf KeyCode = vbKeyV Then ' PASTE
'Get the data previously copied to the clipboard
myData = Clipboard.GetText
If SGGrid1.Editing = True Then
‘Set the data from the clipboard to equal your selected text
SGGrid1.EditSelText = myData
Else
‘Set the data from the clipboard to your selected range
SGGrid1.Selection.Ranges(1).SetData myData, sgFormatCharSeparatedValue, "|", "*"
End If
End If
KeyCode = 0
End If
End Sub
'This sub provides cut funtionality
'This sub clears the contents of the selection
Private Sub ClearRange(theRange As SGRange)
Dim row As Long, col As Long
For row = theRange.TopRow To theRange.BottomRow
For theRange.LeftCol To theRange.RightCol
SGGrid1.CellAt(row, col).Value = ""
Next col
Next row
End Sub
Private Sub SGGrid1_OnInit()
Dim lgRow As Long, lgCol As Long
'Set up the grid and add sample data
With SGGrid1
.DataColCount = 5
.DataRowCount = 10
.MultiSelect = sgMultiSelectExtended
.SelectionMode = sgSelectionFree
.WordWrap = True
For lgRow = 0 To .DataRowCount - 1
.Array(lgRow, 0) = "Test " & (lgRow Mod 5) + 1
For lgCol = 1 To .DataColCount - 1
.Array(lgRow, lgCol) = lgRow & vbCrLf & lgCol
Next lgCol
Next lgRow
.AutoResize = sgNoAutoResize
.AutoSizeRows -1, -1
End With
End Sub
The links below contain additional information about the use of the properties, methods, and events used to provide the Cut, Copy, and Paste functionality as seen in the attached sample.
KeyDown Event
EditSelText
GetData (Selection Ranges)
SetData (Selection Ranges)
If you have any questions or need assistance, please feel free to submit a support request.
Applies To: SharpGrid 2.0
|