Forum > Beginners

Turn OnClick event on and off in code?(Solved)

(1/2) > >>

JLWest:
I have a deck of cards named I1..I52.  (TImage)
CardArray[1].Image:=I1..CardArray[52]:=I52
I need to turn the clicked event on while the hands of Bridge are played out and turned off after the last trick.

I suppose I could write;


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure Tform1.Onclick(S: Integer); begin  Case S: begin                code to turn the event on              end;  end;
Brute force would work but there must be a better way.

speter:
Do you have a variable representing the state-of-the-game (I usually have a variable called "finished" or "game_over").
If you have something like that you could test it's value at the start of the procedure and exit (if the game is over)...

cheers
S.

Handoko:
Disabling/Enabling an object's event can be done by:
- Setting the event to nil and point it back to the event (see the first code)
- Using a variable as mentioned by @speter (see the second code)

Solution 1


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btnEnable1: TButton;    btnDisable: TButton;    btnEnable2: TButton;    btnDisable2: TButton;    Label1: TLabel;    Panel1: TPanel;    Panel2: TPanel;    procedure btnDisable2Click(Sender: TObject);    procedure btnDisableClick(Sender: TObject);    procedure btnEnable1Click(Sender: TObject);    procedure btnEnable2Click(Sender: TObject);    procedure Panel1Click(Sender: TObject);    procedure Panel2Click(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Panel1Click(Sender: TObject);begin  Panel1.Color := Random($FFFFFF);end; procedure TForm1.Panel2Click(Sender: TObject);begin  Panel2.Caption := Random($FFFFFF).ToString;end; procedure TForm1.btnEnable1Click(Sender: TObject);begin  Panel1.OnClick := @Panel1Click;end; procedure TForm1.btnEnable2Click(Sender: TObject);begin  Panel2.OnClick := @Panel2Click;end; procedure TForm1.btnDisableClick(Sender: TObject);begin  Panel1.OnClick := nil;end; procedure TForm1.btnDisable2Click(Sender: TObject);begin  Panel2.OnClick := nil;end; end.
Pay attention on the @ symbol in the code above (see line #51 & #56).

Solution 2


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btnEnable1: TButton;    btnDisable: TButton;    btnEnable2: TButton;    btnDisable2: TButton;    Label1: TLabel;    Panel1: TPanel;    Panel2: TPanel;    procedure btnDisable2Click(Sender: TObject);    procedure btnDisableClick(Sender: TObject);    procedure btnEnable1Click(Sender: TObject);    procedure btnEnable2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure Panel1Click(Sender: TObject);    procedure Panel2Click(Sender: TObject);  private    ClickEnabled: array[1..2] of Boolean;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Panel1Click(Sender: TObject);begin  if ClickEnabled[1] then    Panel1.Color := Random($FFFFFF);end; procedure TForm1.Panel2Click(Sender: TObject);begin  if ClickEnabled[2] then    Panel2.Caption := Random($FFFFFF).ToString;end; procedure TForm1.FormCreate(Sender: TObject);begin  // Enable OnClick on all panels  ClickEnabled[1] := True;  ClickEnabled[2] := True;end; procedure TForm1.btnEnable1Click(Sender: TObject);begin  ClickEnabled[1] := True;end; procedure TForm1.btnEnable2Click(Sender: TObject);begin  ClickEnabled[2] := True;end; procedure TForm1.btnDisableClick(Sender: TObject);begin  ClickEnabled[1] := False;end; procedure TForm1.btnDisable2Click(Sender: TObject);begin  ClickEnabled[2] := False;end; end.
The code above uses an array to remember which item is turn on or off, see line #30. Usually programmers count from 0 for arrays but to make it easier to understand, I used 1 in the code above.

JLWest:
Hi
Hi Handoko

The following code get an error that it's not a member. @CardArray.Image.Click; I tried @CardArray.Image.Click; and @CardArray.ImageClick;


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.CardEventOn; Var  i : Integer;  Image : TImage; begin  for i := 1 to 52 do begin     CardArray[i].Image.OnClick:=@CardArray[i].Image.Click;   // Error  end; end;  

Handoko:
Try this demo:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btnDisable1: TButton;    btnDisable2: TButton;    btnDisable3: TButton;    ImageList1: TImageList;    Label1: TLabel;    procedure CardOnClickEvent(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure btnDisable1Click(Sender: TObject);    procedure btnDisable2Click(Sender: TObject);    procedure btnDisable3Click(Sender: TObject);  private    Cards: array[1..3] of TImage;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.CardOnClickEvent(Sender: TObject);var  Current: TImage;begin  if not(Sender is TImage) then Exit; // Prevent posible error  Current := Sender as TImage;  Current.Picture.Clear;  Application.ProcessMessages;  Sleep(100);  ImageList1.GetBitmap(Random(3), Current.Picture.Bitmap);end; procedure TForm1.FormCreate(Sender: TObject); // Generate and show the cardsvar  i: Integer;begin  for i := 1 to 3 do  begin    // Generate the card    Cards[i] := TImage.Create(Self);    Cards[i].Width   := 86;    Cards[i].Height  := 125;    // Get a random image from ImageList1    ImageList1.GetBitmap(Random(3), Cards[i].Picture.Bitmap);    // Show the card    Cards[i].Parent  := Self;    Cards[i].Top     := 40;    Cards[i].Left    := i * 130 - 60;    // Link the CardOnClicEvent to the card, see line #38    Cards[i].OnClick := @CardOnClickEvent;  end;end; procedure TForm1.btnDisable1Click(Sender: TObject);begin  case (Cards[1].OnClick <> nil) of    True:   begin              btnDisable1.Caption := 'Click to' + #10 + 'Enable';              Cards[1].OnClick    := nil;               // Disable OnClick            end;    False:  begin              btnDisable1.Caption := 'Click to' + #10 + 'Disable';              Cards[1].OnClick    := @CardOnClickEvent; // Encable OnClick            end;  end;end; procedure TForm1.btnDisable2Click(Sender: TObject);begin  case (Cards[2].OnClick <> nil) of    True:   begin              btnDisable2.Caption := 'Click to' + #10 + 'Enable';              Cards[2].OnClick    := nil;               // Disable OnClick            end;    False:  begin              btnDisable2.Caption := 'Click to' + #10 + 'Disable';              Cards[2].OnClick    := @CardOnClickEvent; // Encable OnClick            end;  end;end; procedure TForm1.btnDisable3Click(Sender: TObject);begin  case (Cards[3].OnClick <> nil) of    True:   begin              btnDisable3.Caption := 'Click to' + #10 + 'Enable';              Cards[3].OnClick    := nil;               // Disable OnClick            end;    False:  begin              btnDisable3.Caption := 'Click to' + #10 + 'Disable';              Cards[3].OnClick    := @CardOnClickEvent; // Encable OnClick            end;  end;end; end.

Navigation

[0] Message Index

[#] Next page

Go to full version