Home > Back-end >  blocking user after clicking button more than 3 times
blocking user after clicking button more than 3 times

Time:10-09

I want to block a user from clicking a button again after they have clicked the button more than 3 times. the flow of actions follows

  1. user clicks the button
  2. Code is displayed to user
  3. User gets the opportunity to enter the code in the edit box again
  4. if user requested a new code by clicking the button more than 3 times they will be restricted from clicking the button again.
procedure TVerifyForm.btnCAPTCHAClick(Sender: TObject);
var
  A: integer;
begin
  // CaptchaForm.ShowModal;
  A := 0;
  Inc(A);
  if A = 3 then
  begin
    ShowMessage('BLOCKED');
  end;
end;

CodePudding user response:

just make a variable like fhitcount : intger;

on form create you put this code fhitcount := 3;

onclick event this code.

begin
  fhitcount := fhitcount-1;
  if fhitcount = 0 then
    begin
      //do what you want and set fhitcount := 3; 
    end;
end;

so every time the onclick event is triggerd the fhitcount integer goes down with -1. when he hits 0 then he activates the next code and resets the fhitcount to 3. if you want.

CodePudding user response:

You are using a local variable, that will not work in this case. You need a variable that persists after the OnClick handler exits. Make it a member of the button's parent Form.

type
  TVerifyForm = class(TForm)
    ...
  private
    NumberOfClicks: Integer;
    ...
  end;

procedure TVerifyForm.btnCAPTCHAClick(Sender: TObject);
begin
  // CaptchaForm.ShowModal;
  Inc(NumberOfClicks);
  if NumberOfClicks > 3 then
  begin
    ShowMessage('BLOCKED');
  end;
end;

Or, you could simply use the button's own Tag property instead:

procedure TVerifyForm.btnCAPTCHAClick(Sender: TObject);
var
  Btn: TButton;
begin
  Btn := Sender as TButton;
  // CaptchaForm.ShowModal;
  Btn.Tag := Btn.Tag   1;
  if Btn.Tag > 3 then
  begin
    ShowMessage('BLOCKED');
  end;
end;
  • Related