Cancel the rest of the procedure if InputQuery is false

procedure TForm1.Button2Click(Sender: TObject);
var
  Button: TButton;
  Example : String;

begin

  if {Example = ''} InputQuery('Put a question/request here', Example) then
  Repeat                                                           

    InputQuery('Put a question/request here', Example);

if InputQuery = False then
 Abort
 else
  Until Example <> ''; //or InputBox.


  Button := TButton.Create(self);
  Button.Parent := self;
  //Button properties go here
  Button.Caption := (Example);
  //Any procedures can go here

end;

This procedure continues after a repeat cycle, even if the user presses the cancel button. I tried using the GoTo function using the label CancelCreateButtonif InputQuery = False, but I just get errors (so I deleted some of the code).

How can I make it so that if the user clicks cancel on the inputquery, he cancels the procedure and does not create a button?

0
source share
3 answers

If the Cancel button of the input request form is clicked , the call InputQueryreturns False. In this scenario, you must raise a Abortsilent exception to skip the rest of the event handler.

if not InputQuery(...) then
  Abort;

, :

repeat
  if not InputQuery(..., Name) then
    Abort;
until NameIsValid(Name);
+2

Exit

+1

InputQuery, , ; InputQuery . - :

procedure TForm1.Button2Click(Sender: TObject);
var
  FSeatButton: TButton;
  Name : String;
  InputOK : Boolean;
  Label
  CancelCreateButton;
begin
  InputOK := InputQuery('Enter Students Name', 'Name', Name);
    // You can add check on the validity of the student name 
    // (e.g, is it a duplicate) here and update the value of InputOK 
    // (to False) if you don't want the button creation to go ahead

  if InputOK then begin
    FSeatButton := TButton.Create(self);
    FSeatButton.Parent := self;
    FSeatButton.Left := 100;
    FSeatButton.Top := 100;
    FSeatButton.Width := 59;
    FSeatButton.Height := 25;
    FSeatButton.Caption := (Name);
    FSeatButton.OnMouseDown := ButtonMouseDown;
    FSeatButton.OnMouseMove := ButtonMouseMove;
    FSeatButton.OnMouseUp := ButtonMouseUp;
  end;
end;

, , , repeat ... until, .

0

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


All Articles