Delphi tips:

Disable and Enable an Event Handler

Sometimes you want to disable an event handler after the FIRST time that the event is "fired". Example: you want to disable all clicks on Label1 after it has been clicked:

procedure TForm1.Label1OnClick(Sender: TObject);
begin
  // Code to execute, e.g.:
  Label1.Caption := 'Label1 has been clicked';
  //...
  // Disable future clicks on Label1:
  Label1.OnClick := nil;
end;   

Maybe at some later time, you want to re-enable the label's OnClick event handler. Code it like this:

procedure TForm1.Button1OnClick(Sender: TObject);
begin
  // Code to execute, e.g.:
  Label1.Caption := 'Click on this text';
  //...
  // Re-enable click events for Label1:
  Label1.OnClick := Label1OnClick;
end;

For a Button, Checkbox, RadioGroup... you can obtain the same effect by disabling the component itself, for example: Button1.Enabled := FALSE;  -- at the same time, you give visual feedback to the user. The trick above makes only sense for other components, such as Panel, Label, Form,...


 

DelphiLand Club Members of the DelphiLand Club receive our Crash Course Delphi, plus the fully commented source code of numerous projects, plus guaranteed answers from our Delphi Forum. Membership is for life!

TOP   DelphiLand Club Membership  DC Library  Forum  Forum Archives  Crash Course Delphi  Tips  Source Code  Downloads  Links