[Solved]Is it possible to intercept Copy2Clipboard? AND MVP-app lazarus / 31-LCL ---------------------------------------------------------------------- cdbc · 2025-10-06 14:27 Hi In an effort to control what gets inserted into the clipboard, when I push Ctrl + C in a TStringGrid, by default it inserts the whole row of cells separated by #9, whereas I want to e.g.: just insert the value of 1 of the cells... Is it possible to intercept the default behaviour?!? ...and how?  Regards Benny ---------------------------------------------------------------------- Aruna · 2025-10-06 14:38 Quote from: cdbc on October 06, 2025, 04:27:38 pmHi In an effort to control what gets inserted into the clipboard, when I push Ctrl + C in a TStringGrid, by default it inserts the whole row of cells separated by #9, whereas I want to e.g.: just insert the value of 1 of the cells... Is it possible to intercept the default behaviour?!? ...and how?  Regards Benny Hi Benny, tested and woiks! 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.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin  // Detect Ctrl+C  if (Key = Ord('C')) and (ssCtrl in Shift) then  begin    // Prevent default TStringGrid copy behavior    Key := 0;     // Now put your custom clipboard handling here    if StringGrid1.Col >= 0 then    begin      Clipboard.AsText := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row];      ShowMessage('Copied cell value: ' + Clipboard.AsText);    end;  end;end; ---------------------------------------------------------------------- Hartmut · 2025-10-06 14:46 For the purpose to copy selectable parts of a TStringGrid into the clipboard I have created a common PopupMenu which allows to select:  - copy whole StringGrid  - copy complete row of the cursor  - copy complete column of the cursor  - copy only the selected field  - copy the selected part of the StringGrid Maybe this is an alternative for you. ---------------------------------------------------------------------- cdbc · 2025-10-06 15:08 Hi Aruna Cool mate, thanks. I'll give it a try and report back  @Hartmut: if that doesn't work I'll try yours... Regards Benny ---------------------------------------------------------------------- Aruna · 2025-10-06 15:27 Quote from: Hartmut on October 06, 2025, 04:46:55 pmFor the purpose to copy selectable parts of a TStringGrid into the clipboard I have created a common PopupMenu which allows to select:  - copy whole StringGrid  - copy complete row of the cursor  - copy complete column of the cursor  - copy only the selected field  - copy the selected part of the StringGrid Maybe this is an alternative for you. Hi Hartmut, where do I find this PopupMenu you have created please? ---------------------------------------------------------------------- Aruna · 2025-10-06 15:29 Quote from: cdbc on October 06, 2025, 05:08:41 pmHi Aruna Cool mate, thanks. I'll give it a try and report back  8) @Hartmut: if that doesn't work I'll try yours... Regards Benny Benny I have attached a zip for you to play with.  Have fun! ---------------------------------------------------------------------- cdbc · 2025-10-06 17:05 Hi Aruna Thanks mate =^, works like a charm  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 unit view.fra_showitem;{$mode ObjFPC}{$H+}interfaceuses  Classes, SysUtils, Forms, Graphics, Controls, ComCtrls, Grids, ExtCtrls, model.intf; type  { TfraShowItem }  TfraShowItem = class(TFrame)    grdNvn: TStringGrid;    btnClose: TPanel;    lblTitleVal: TPanel;    lblExtraVal: TPanel;    pnlTiRight: TPanel;    pnlExtraId: TPanel;    pnlTitleId: TPanel;    pnlTiLeft: TPanel;    pnlTiEx: TPanel;    pnlBottom: TPanel;    pnlHead: TPanel;    procedure btnChooseClick(Sender: TObject);    procedure btnChooseMouseEnter(Sender: TObject);    procedure btnChooseMouseLeave(Sender: TObject);    procedure btnCloseClick(Sender: TObject);    procedure btnCloseMouseEnter(Sender: TObject);    procedure btnCloseMouseLeave(Sender: TObject);    procedure grdNvnDblClick(Sender: TObject);    procedure grdNvnKeyDown(Sender: TObject; var Key: Word; {%H-}Shift: TShiftState);    procedure grdNvnResize(Sender: TObject);    procedure grdNvnSelection(Sender: TObject; {%H-}aCol, {%H-}aRow: Integer);  private    fActiveTrx: ImemTransaction; { to keep our current item }    fOwner: TWinControl;    fParentForm: TForm;    fViMo: ImemViewModelMain;    procedure FrameShow(Sender: TObject);    procedure SetupFrame;  protected    procedure CopyValueToClip(aGridRow: cardinal);  public    procedure BeforeDestruction; override; { should get called just before we're out of business }    procedure Init(aViMo: ImemViewModelMain;anItem: ImemItem); { constructor / oncreate }  end; { CreateShowFrame instantiates a show-frame in the anOwner(TTabSheet) provided  and works on the anItem provided, result is the new frame;  Caution: DO NOT pass nil in any parameter!!! }function CreateShowFrame(anOwner: TWinControl; aViMo: ImemViewModelMain; anItem: ImemItem): TfraShowItem; implementation { bc_messages provides post/send-message + LM_CLOSETABSHEET etc... }uses bc.advstring, bc.pcthelp, bc.grdhelp, bc.messages, model.decl, clipbrd; function CreateShowFrame(anOwner: TWinControl; aViMo: ImemViewModelMain; anItem: ImemItem): TfraShowItem;begin  Result:= TfraShowItem.Create(anOwner); { anOwner is the tab/page and will free us }  Result.Parent:= anOwner; { anOwner is also the tab/page that will draw us }  Result.fOwner:= anOwner;  TTabSheet(Result.fOwner).OnShow:= @Result.FrameShow;  Result.fParentForm:= bcGetParentForm(anOwner);       //GetParentForm(Self); from forms.pp  Result.Name:= 'ShowItem';  Result.Align:= alClient;  Result.Init(aViMo,anItem);end; {$R *.lfm}{ TfraShowItem }procedure TfraShowItem.btnCloseClick(Sender: TObject);begin // post msg with pg in wparam & -1 in lparam  try    if fOwner is TTabSheet then begin      if fParentForm <> nil then        bcPostMessage(fParentForm.Handle,LM_CLOSETABSHEET,ptrint(fOwner),-1); { trickery }    end;  except end;end; procedure TfraShowItem.btnCloseMouseEnter(Sender: TObject);begin  with (Sender as TPanel) do begin    Font.Color:= clYellow;    Font.Style:= [fsBold];  end;end; procedure TfraShowItem.btnCloseMouseLeave(Sender: TObject);begin  with (Sender as TPanel) do begin    Font.Color:= clAqua;    Font.Style:= [];  end;end; procedure TfraShowItem.grdNvnDblClick(Sender: TObject);begin  btnCloseClick(Sender);end; procedure TfraShowItem.grdNvnKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);var ct: boolean;begin  ct:= (ssCtrl in Shift);  case Key of                         $08: btnCloseClick(Sender); // backspace  $0D: btnCloseClick(Sender); // enter    $20: btnCloseClick(Sender); // space    $1B: btnCloseClick(Sender); // escape    $43: if ct then begin           CopyValueToClip(grdNvn.Row);// ctrl + C           Key:= 0;         end;  end;  if grdNvn.CanSetFocus then grdNvn.SetFocus; {=^}end; procedure TfraShowItem.grdNvnResize(Sender: TObject);var sixth: word = 0;begin  sixth:= ((grdNvn.Width-25) div 6); // 25 for scrollbar, 1part, 2part & 3part  if grdNvn.ColCount >= 3 then begin    grdNvn.Columns[0].Width:= sixth+50;    grdNvn.Columns[1].Width:= sixth * 2;    grdNvn.Columns[2].Width:= (sixth * 3)-50;  end;         if grdNvn.CanSetFocus then grdNvn.SetFocus; {=^}end; procedure TfraShowItem.grdNvnSelection(Sender: TObject; aCol, aRow: Integer);begin  if grdNvn.CanSetFocus then grdNvn.SetFocus; {=^}end; procedure TfraShowItem.btnChooseClick(Sender: TObject); /// not in use, but kept for referencebegin // post msg with pg in wparam & id i lparam  try    if fOwner is TTabSheet then begin      if fParentForm <> nil then        bcPostMessage(fParentForm.Handle,LM_CLO SETABSHEET,ptrint(fOwner),fActiveTrx.ID); { trickery }    end;  except end;end; procedure TfraShowItem.btnChooseMouseEnter(Sender: TObject); /// not in use, but kept for referencebegin  with (Sender as TPanel) do begin    Font.Color:= clYellow;    Font.Style:= [fsBold];  end;end; procedure TfraShowItem.btnChooseMouseLeave(Sender: TObject); /// not in use, but kept for referencebegin  with (Sender as TPanel) do begin    Font.Color:= clAqua;    Font.Style:= [];  end;end; procedure TfraShowItem.FrameShow(Sender: TObject);begin  if grdNvn.CanSetFocus then grdNvn.SetFocus; {=^}end; procedure TfraShowItem.SetupFrame;var sixth: word = 0; i: integer; s: string;begin  lblTitleVal.Caption:= fActiveTrx.Title;  lblExtraVal.Caption:= fActiveTrx.Extra;  bcGridClear(grdNvn,false,false);  sixth:= ((grdNvn.Width-25) div 6); // 25 for scrollbar  bcGridCreateHeaders(grdNvn,['Name','Value','Note' ],[sixth+50,sixth*2,(sixth*3)-50]);  grdNvn.TitleFont.Color:= clAqua;  grdNvn.TitleFont.Style:= [fsBold]; //???  for i:= 0 to fActiveTrx.Items.Count-1 do begin    s:= fActiveTrx.Items[i];    bcGridSetRowText(grdNvn,-1,[bcGetStrField(0,s,'|'),                              bcGetStrField(1,s,'|'),                                bcGetStrField(2,s,'|')]);  end;  if grdNvn.CanSetFocus then grdNvn.SetFocus; {=^}end; procedure TfraShowItem.CopyValueToClip(aGridRow: cardinal);var ls: string;begin  ls:= fActiveTrx.Items[aGridRow-1]; { grids count /real/ values from 1 }  ls:= bcGetStrField(1,ls,'|'); { pick 'Value' from the entire row's text }  Clipboard.AsText:= ls;        { now insert the text into global clipboard }  fViMo.Provider.NotifyConsumers(13,nil,bcStrNew('(i) "'+ls+'" copied to clipboard')); // 13 = prStatusend; procedure TfraShowItem.BeforeDestruction;begin  if Assigned(fActiveTrx) then fActiveTrx.Obj.Free;  inherited BeforeDestruction;end; procedure TfraShowItem.Init(aViMo: ImemViewModelMain;anItem: ImemItem);begin  if anItem <> nil then begin    fViMo:= aViMo;    fActiveTrx:= TmemTransaction.Create;    fActiveTrx.AssignFrom(anItem);    pnlHead.Caption:= 'Memento #'+fActiveTrx.ID.ToString+', last edited: '+fActiveTrx.DateTime;    pnlBottom.Caption:= memShowItemBottomCapt; { was:  Press [ Space,  Escape,  Enter,  BackSpace ] to return }    SetupFrame;  end else btnCloseClick(btnClose);end; end. Regards Benny ---------------------------------------------------------------------- Hartmut · 2025-10-06 17:25 Quote from: Aruna on October 06, 2025, 05:27:57 pmHi Hartmut, where do I find this PopupMenu you have created please? Hello Aruna, I did not publish this PopupMenu. It consists only of  - 1 procedure to populate a TPopupMenu with 5 TMenuItem's, which all are assigned the same given OnClick-Event (but with different 'Tag'-values)  - 1 procedure, which is called by obove OnClick-Event and copies (depending of the 'Tag'-value) the wanted part of the StringGrid into the clipboard. I hesitate to publish the code, because it has German comments and uses a couple of personal functions and types, which I all had to declare or replace. I think it should be not to difficult, to create those 2 procedures. ---------------------------------------------------------------------- jamie · 2025-10-06 22:04 Did I miss something? 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.StringGrid1CellProcess(Sender: TObject; aCol, aRow: Integer;  processType: TCellProcessType; var aValue: string);begin  If ProcessType = cpCopy Then    Begin      Caption := AValue;    end;end;                                                       That only gets the current cell, not the whole row. Of course, you can modify the string while you are there, too! Jamie ---------------------------------------------------------------------- cdbc · 2025-10-07 07:15 Hi Thanks jamie, I learnt something new there... Regards Benny ---------------------------------------------------------------------- Aruna · 2025-10-07 11:25 Quote from: cdbc on October 06, 2025, 07:05:34 pmHi Aruna Thanks mate =^, works like a charm  Hi Benny, I am happy you got things working but... I’m yet to start using TFrames, so this code is way above my current level of familiarity and understanding with Lazarus and Free Pascal. And in uses you have a 'model.int' ftw is that? If this isn’t a closed-source project, I’d love to see the full application, or even just a couple of screenshots if that’s possible. Or put it up on GitHub if that is allowed and ok? Or tell me how do I test your code? Because right now I have no clue where to start doing so.. ---------------------------------------------------------------------- Aruna · 2025-10-07 11:27 Quote from: Hartmut on October 06, 2025, 07:25:03 pmQuote from: Aruna on October 06, 2025, 05:27:57 pmHi Hartmut, where do I find this PopupMenu you have created please? Hello Aruna, I did not publish this PopupMenu. It consists only of  - 1 procedure to populate a TPopupMenu with 5 TMenuItem's, which all are assigned the same given OnClick-Event (but with different 'Tag'-values)  - 1 procedure, which is called by obove OnClick-Event and copies (depending of the 'Tag'-value) the wanted part of the StringGrid into the clipboard. I hesitate to publish the code, because it has German comments and uses a couple of personal functions and types, which I all had to declare or replace. I think it should be not to difficult, to create those 2 procedures. Hi Hartmut, Hello, and thank you for the explanation! That makes perfect sense. No worries at all about not publishing it. I completely understand if it’s using personal functions and German comments. Your description is already very helpful, and I think I can put something similar together based on that. I really appreciate you taking the time to outline how it works! ---------------------------------------------------------------------- Aruna · 2025-10-07 11:29 Quote from: cdbc on October 07, 2025, 09:15:44 amHi Thanks jamie, I learnt something new there... Regards Benny Yes me too I never knew we had : StringGrid1CellProcess ---------------------------------------------------------------------- Aruna · 2025-10-07 11:43 Benny, I ran your unit through chatGPT and it generated this: TFrame Breakdown so now I have a fair understandiing of what your doing but things are still very foggy to me ---------------------------------------------------------------------- cdbc · 2025-10-07 13:34 Hi Aruna Hehehe, how COOL is that then... Please send my regards to ChatGPT, for a nice write-up  Right, So this little project is/was actually my very first _real_ MVVM project after I read a book about these patterns, it's the preamble to my research into (M)odel (V)iew (P)resenter frameworks, for which I've later implemented a setup-utility 'MVP-Setup' that delivers you a ready-made connected and compilable (&runable) skeleton application bundle... It has a sibling, coded for the command-line with a TUI user interface that employs the 'Viewmodel' & 'Models' etc. i.e.: the entire backend of 'Memento' , with only the UI as difference  Both projects employs my 'Plugin' framework ((P)lug(I)n (S)ervices (S)olution) to further separate concerns into  j.i.t. loadable modules... Furthermore, they boast 256 bit AES encryption of data and detailed logging... I'll see if I can make it available to you, if you want that ofc? Regards Benny ---------------------------------------------------------------------- cdbc · 2025-10-07 14:03 Hi Aruna I'll see if I can invite you into the gitlab repo, could you send me your email in a PM? Regards Benny Screenshot added