Copy and paste rows in a stringgrid lazarus / 31-LCL ---------------------------------------------------------------------- bobonwhidbey · 2021-10-02 18:05 When I copy and paste in my stringgrid, it overwrites existing rows. The number of new lines held in the ClipBoard is easily obtained with SelectRowCount := Grid.SelectedRangeCount;  What I need to do (I think) is add some blank lines, below Grid.Row which will be able to receive the pasted rows. I've tried, unsuccessfully, to use the Grid's OnKeyUp event: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.GridKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);begin  if (ssCtrl in Shift) and (Key = VK_V) then // paste      now what??????end; I think the rows are already copied by the time the ctrl-V is processed.  How can I copy the rows from the Grid's Clipboard underneath the selected row, without overwriting any of the existing rows? ---------------------------------------------------------------------- Joanna · 2021-10-06 15:04 I've never tried to copy into a stringgrid but i know you have to add rows first here is some code of how to do it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROCEDURE TFRAME_STRING_GRID_2021.ADD_ROW( CONST ROW_NUMBER: WORD; CONST STRINGS: TSTRINGARRAY) ;            var X:integer; BEGIN WITH GRID DO      BEGIN      WITH Columns DO           FOR X:= Count TO HIGH(STRINGS) DO               BEGIN               Add;               Columns[X].Alignment := taCenter;               END;      IF RowCount < ROW_NUMBER + 1         THEN RowCount := ROW_NUMBER + 1;      FOR X:= 0 TO HIGH(STRINGS) DO          Cells[X + Ord(FixedCols = 1),ROW_NUMBER]:= STRINGS [X];      IF goFixedRowNumbering IN Options       THEN cells [0,ROW_NUMBER]:= ROW_NUMBER.ToString;      END; END; ---------------------------------------------------------------------- bobonwhidbey · 2021-10-06 20:00 If you leave the StringGrid option goEditing unchecked then you are able to handle the Ctrl-V (paste) exactly how you want. In the desired (IMO) way, this would mean adding the selected rows directly below the current row. It's under your control.  However, with goEditing unchecked, you lose out on all the default editing capabilities of a stringgrid cell. If you check goEditing, you get the desired editing capabilities but you lose control over all paste capabilities. As far as I can tell, the only 'solution' is to leave goEditing unchecked and then roll your own cell editing capabilities as well as copy/paste capabilities. I was hoping there was an event that would let me stop Lazarus from performing the paste function when goEditing was checked, but I guess not. ---------------------------------------------------------------------- Joanna · 2021-10-06 21:53 If you have the inclination it’s possible to make your own class that inherits tstringgrid and override the methods that aren’t doing what you want. After you get the descendant class created you can find the the ancestor code and copy it and modify it ---------------------------------------------------------------------- wp · 2021-10-06 22:01 Quote from: bobonwhidbey on October 02, 2021, 08:05:58 pmI've tried, unsuccessfully, to use the Grid's OnKeyUp event: How can I copy the rows from the Grid's Clipboard underneath the selected row, without overwriting any of the existing rows? In the attached sample project I am subclassing the TStringGrid in order to override the DoPasteFromClipboard method. At first I determine whether the clipboard sends a fully selected row or column and, in this case, add an empty row/column and paste the clipboard into these new cells. Otherwise there is a normal paste overwriting the destination cells. Of course you can change the logic here in any way needed. I disagree that Ctrl-V works without goEditing. This is clear from the original TStringGrid.DoPastefromClipboard: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomStringGrid.DoPasteFromClipboard;begin  if HasMultiSelection then    exit;   if EditingAllowed(Col) then  // <--- Editing required, otherwise clipboard actions are skipped.  begin    if Clipboard.HasFormat(CF_TEXT) and not Clipboard.HasFormat(CF_HTML) then SelectionSetText(Clipboard.AsText);    if Clipboard.HasFormat(CF_TEXT) and Clipboard.HasFormat(CF_HTML) then SelectionSetHTML(Clipboard.GetAsHtml(True), Clipboard.AsText);  end;end; ---------------------------------------------------------------------- Joanna · 2021-10-06 22:23 Is there a way to intercept the text in the onpaste event and then put it into some format that can be entered programmatically? How is it determined which text goes into which cell? ---------------------------------------------------------------------- bobonwhidbey · 2021-10-06 23:29 What I meant is that it's possible to write your own code for copying and pasting. If goEditing is checked then you will end up pasting the data twice; once with your own code and once with the stringgrid code. The only way to prevent the stringgrid from pasting is to uncheck goEditing. Thank you very much for the code. I'll dig into it tomorrow. ---------------------------------------------------------------------- Zvoni · 2021-10-07 07:30 Use OnKeyDown....... ---------------------------------------------------------------------- wp · 2021-10-07 09:00 Of course. The OnKeyDown event is more accessible than subclassing... Here is the code from above modified such that it uses the OnKeyDown. It first detects whether CTRL-V is pressed and whether the clipboard contains text (this check was missing from the other code). Then it checkes whether the clipboard contains a full row or full column and, if true, inserts an empty row/column and moves the active cell to the start of this row/column. The inherited method, finally, pastes the clipboard at his new location. @Joanna: The paste operation goes to the position of the active cell (StringGrid.Col, StringGrid.Row). When copying cells to the clipboard, the LCL grids put a tab character between cells in the same row, and a Lineending string (#13#10 on Windows) at the end of each row. The pasting process expects exactly these delimiters in order to be able to separate the individual cells. In addition to the text format, the LCL grids also copy a html-coded string to the clipboard (not sure at which version this came in). ---------------------------------------------------------------------- bobonwhidbey · 2021-10-07 17:28 WP, your program handles copying cells, whether full or partial rows, very nicely. My problem is perhaps centered around the meaning of "Paste". In all of the text editing programs I'm familiar with, the Ctrl-C / Ctrl-V Copy/Paste operations will INSERT the copied text. They do not overwrite text. OTOH, it is most logical for the Copy/Paste operations to behave as you have programmed them when dealing with a stringgrid. For my app, I think the most logical way to proceed is to keep Copy/Paste as you have programmed and then include a separate "Insert Row(s)" button to handle the addition or rows followed by a paste of the copied cells. It's easy enough to add the correct number of rows below the current row. What I then need to do in the "InsertRowsClick" event is to trigger the Ctrl-V event in real time. How would I do that? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm2.InsertRowsClick(Sender: TObject);var  sa: TStringArray;  s: string;  i : integer;begin  s := Clipboard.AsText;  sa := s.Split([Lineending]);  i := 0;  while sa[i] <> '' do  begin    StringGrid1.InsertRowWithValues(StringGrid1.Row+1, ['']);    Inc(i);  end;end;