Inputbox lock CTRL+V

Asked

Viewed 790 times

2

I have the following code:

vResultado := InputBox(Application.Title, 'Leia o cartão de Segurança:', '');

There is a way to block copy and paste in Inputbox?

2 answers

4


I believe there’s no such thing as InputBox. You can create your own InputBoxand with that you will be able to define the events you want. In this case we create a new Unit, which will be our class, with a main class method (class Function) called Inputbox. Follow 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, Ex: Ctrl+C, Ctrl+X, Right Click + Paste ...

  • Victor +1, I have no access to Delphi at this time, but... I believe it is possible to modify the InputQuerry native, I mean, add to it the event KeyPress personalized! A InputBox this written in Vcl.Dialogs, take a look after!

  • @Junior, tampering with the Delphi code would not be cool. What I’ve been thinking, is to create a Helper for this guy, what do you think ? At the moment I’m out of time, as soon as I get it I see if it would work.

  • I edited the answer by creating a specific class for Inputbox, this will make it easier to use the method. Also, I set it to work with Inputbox, and no longer with Inputquery.

  • 1

    Ready, simpler and functional. About tinkering with Delphi code, really "not a good practice" but for several cases is even better!

-3

is simple, in the event KeyPress of your input, you put the following code

if (Key=#22) or (Key=#3) then Key:=#0;   // 22 = [Ctrl+V] / 3 = [Ctrl+C]
  • 2

    I think there is no Keypress event, because it is an Inputbox

  • Simple, create a new form and add an input.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.