• 0

Delphi / Pascal Generic Event Handlers


Question

Right, I'm creating a hangman game using VLE Forms in Turbo Delphi 2006.

What I want to do it write a generic event handler so that I don't need to have 26 EH's doing the same thing when the only difference is the caption on the button that will be clicked.

I know theres a way of doing this, and it's got something to do with

if ????? (Sender as TButton).Caption ??? then ????

but thats all I know.

Help, please!!

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

define a procedure

type
  TfrmMain = class(TForm)
  procedure btnGenericClick(Sender: TObject);
end;

{------------------------------------------------------------------------------}
procedure TfrmMain.btnGenericClick(Sender: TObject);
begin
   if (Sender is TButton) then
	 begin
	   (Sender as TButton).Caption = 'Hi';
	 end;
end;

point all your button handlers OnClickEvent to btnGenericClick();

you can do this in code too

button1.OnClick = btnGenericClick;

button2.OnClick = btnGenericClick;

button3.OnClick = btnGenericClick;

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.