Link to home
Start Free TrialLog in
Avatar of ISGDude
ISGDude

asked on

Reusable Click routines

I have a form with a lot of TLabels on it.  The label will display a '1' or '0' for a data stream.  When the label is clicked, it will change logic levels.

procedure TForm2.L_R0D4DblClick(Sender: TObjectl);
begin
If form2.L_R0D4D.Caption = '0' then form2.L_R0D4.caption := '1'
                                              else form2.L_R0D4.caption := '0' ;
end ;

Is there an easy way to assign the same Click event to all of the TLabels on the form?
The event would have to change a different TLabel caption.  I'm trying to get away from having a 100+ onclick procedures and replace it with 1.


I was thinking something like this:

procedure ToggleBitLabel (Sender :TLabel) ;
begin
If Sender.Caption = '0' then Sender.caption := '1'
                                  else Sender.caption := '0'  ;
end ;

This one does not work because it can not be declared in the "Type" heading and needs to be decared in the "var" heading.

Thanks
Randy






ASKER CERTIFIED SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaneholmes
shaneholmes


procedure ToggleBitLabel (Sender :TLabel) ;
begin
If TLabel(Sender.Caption) = '0' then
 TLabel(Sender).caption := '1'
else
 TLabel(Sender).caption := '0'  ;
end ;

Then assign this event to all


Shane
hmmmm, wht the 'B' grade was that not what you wanted?

Shane
Actually, you could test it as well

 if (Sender is TLabel) AND  (TLabel(Sender).Caption = '0') then
  TLabel(Sender).Caption:= '1'
 else
  TLabel(Sender).Caption:= '0';


Shane  
Avatar of ISGDude

ASKER

Sorry for the B grade.  I thought since the question was so simple.......
Is there a way to go back and change it?

Anyway,  The 1st suggestion worked when declared under

type
  TForm2 = class(TForm)
   L_R0D4: TLabel;
  Procedure L_R0D4Click (Sender : TObject) ;

And I set all of the TLabels to use this Procedure.

For the 2nd suggestion,  where do I declare it so that I can assign it to an click event?


I thought maybe you weren't sure about the grading that's why i asked. Don't worry about it.

Just remember next time, you grading on the contant they provide you, and how useful it is. Not on hte simplicity. Thats what the points are for.

OK

Originally i suggested

procedure ToggleBitLabel (Sender :TLabel) ;
begin
If TLabel(Sender.Caption) = '0' then
 TLabel(Sender).caption := '1'
else
 TLabel(Sender).caption := '0'  ;
end ;



then, what I was suggesteing was this:

procedure ToggleBitLabel (Sender :TLabel) ;
begin
 if Sender is TLabel then
  if TLabel(Sender.Caption) = '0' then
   TLabel(Sender).caption := '1'
  else
   TLabel(Sender).caption := '0'  ;
end ;



Shane