I have the following code:
vResultado := InputBox(Application.Title, 'Leia o cartão de Segurança:', '');
Is there a way to block copy and paste in the InputBox?
I have the following code:
vResultado := InputBox(Application.Title, 'Leia o cartão de Segurança:', '');
Is there a way to block copy and paste in the InputBox?
I do not think there is anything like this for InputBox
.
You can create your own InputBox
and with this you will be able to define the events that you want.
In this case we create a new unit, which will be our class, with a main method of type class (class function) called InputBox. Here is the code below:
unit Unit2;
interface
type
TCustomInputBox = class
private
class procedure EditKeyPress(Sender: TObject; var Key: Char);
public
class function InputBox(const ACaption, APrompt, ADefault: string): string;
end;
implementation
uses
Vcl.Forms, Vcl.StdCtrls, System.Types, System.UITypes;
{ TCustomInputBox }
class procedure TCustomInputBox.EditKeyPress(Sender: TObject; var Key: Char);
begin
if (Key = #22) then
Key := #0;
end;
class function TCustomInputBox.InputBox(const ACaption, APrompt, ADefault: string): string;
var
Form: TForm;
Prompt: TLabel;
Edit: TEdit;
DialogUnits: TPoint;
ButtonTop, ButtonWidth, ButtonHeight: Integer;
IsValid: Boolean;
begin
IsValid := False;
Form := TForm.Create(nil);
with Form do
try
ClientWidth := 350;
ClientHeight := 85;
Canvas.Font := Font;
BorderStyle := bsDialog;
Caption := ACaption;
Position := poScreenCenter;
Prompt := TLabel.Create(Form);
with Prompt do
begin
Parent := Form;
Caption := APrompt;
Left := 10;
Top := 10;
WordWrap := True;
end;
Edit := TEdit.Create(Form);
with Edit do
begin
Parent := Form;
Left := Prompt.Left;
Top := Prompt.Top + Prompt.Height + 5;
Width := Form.Width - 24;
Text := ADefault;
OnKeyPress := EditKeyPress;
SelectAll;
end;
ButtonTop := Edit.Top + Edit.Height + 25;
ButtonWidth := 75;
ButtonHeight := 25;
with TButton.Create(Form) do
begin
Parent := Form;
Caption := 'OK';
ModalResult := mrOk;
Default := True;
SetBounds(Form.Width - 180, 60, ButtonWidth,
ButtonHeight);
end;
with TButton.Create(Form) do
begin
Parent := Form;
Caption := 'Cancel';
ModalResult := mrCancel;
Cancel := True;
SetBounds(Form.Width - 90, 60,
ButtonWidth, ButtonHeight);
end;
if ShowModal = mrOk then
begin
Result := Edit.Text;
end;
finally
Form.Free;
end;
end;
end.
From there, just use it anywhere in your project:
procedure TForm1.Button1Click(Sender: TObject);
begin
TCustomInputBox.InputBox(Application.Title, 'Leia o cartão de Segurança:', '');
end;
Remember that other locks can also be implemented, eg Ctrl + C, Ctrl + X, Right Button + Paste ...
It's simple, in the KeyPress
event of your input
, you put the following code
if (Key=#22) or (Key=#3) then Key:=#0; // 22 = [Ctrl+V] / 3 = [Ctrl+C]