How to get a notification when the text pasted from the clipboard into the edit field exceeds the maximum length?

I need to show a message when the text pasted from the clipboard into the control TEditexceeds the maximum length.

How to do it?

+4
source share
3 answers

A subclass of the editing class and listening for the WM_PASTE message . For example (for Unicode Delphi):

uses
  Vcl.Clipbrd;

type
  TEdit = class(Vcl.StdCtrls.TEdit)
  private
    procedure WMPaste(var Msg: TWMPaste); message WM_PASTE;
  end;

implementation

procedure TEdit.WMPaste(var Msg: TWMPaste);
begin
  if (MaxLength > 0) and (Length(Clipboard.AsText) > MaxLength) then
    ShowMessage('Text is too long!');
  inherited;
end;

Another, slightly more efficient version of WinAPI (which does not copy the clipboard, but simply asks for its size), could be (also for Unicode Delphi):

type
  TEdit = class(Vcl.StdCtrls.TEdit)
  private
    procedure WMPaste(var Msg: TWMPaste); message WM_PASTE;
  end;

implementation

procedure TEdit.WMPaste(var Msg: TWMPaste);
var
  Data: THandle;
begin
  if (MaxLength > 0) and OpenClipboard(0) then
  try
    Data := GetClipboardData(CF_UNICODETEXT);
    if (Data <> 0) and ((GlobalSize(Data) div SizeOf(Char)) - 1 > MaxLength) then
      ShowMessage('Text is too long!');
  finally
    CloseClipboard;
  end;
  inherited;
end;

If you want the text to not be inserted into the control, remove the inherited calls from the above examples.

+6

:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Clipbrd;

type
  TPasteEdit = class(TEdit)
    private
      FMaxPaste: Integer;
      procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
    protected
    public
      constructor Create(AOwner: TComponent); override;
    published
      property MaxPaste: Integer read FMaxPaste write FMaxPaste default 0;
      //You can define it or use MaxLength as well
  end;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



{ TPastEdit }

constructor TPasteEdit.Create(AOwner: TComponent);
begin
  inherited;
  FMaxPaste:= 0;
end;

procedure TPasteEdit.WMPaste(var Message: TWMPaste);
begin
  if Length(Clipboard.AsText) > FMaxPaste then
    ShowMessage('The text you paste is too long!') // Call inherited if you want to paste anyway
      else
        inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
Var
  PasteEdit: TPasteEdit;
begin
  PasteEdit:= TPasteEdit.Create(Self);
  PasteEdit.Parent:= Self;
  PasteEdit.MaxPaste:= 3;
end;

end.
+4

Subclass TEditto handle notification EN_MAXTEXT:

Sent when the current text insertion exceeds the specified number of characters for the edit control. Text insertion has been truncated.

This applies to both input and insertion, and takes into account the choice of text for you. For instance:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TEdit = class(Vcl.StdCtrls.TEdit)
  private
    procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TEdit.CNCommand(var Message: TWMCommand);
begin
  inherited;
  if Message.NotifyCode = EN_MAXTEXT then
    ShowMessage('Too much text!');
end;

end.
+2
source

Source: https://habr.com/ru/post/1696348/


All Articles