Solved

Reusable Click routines

Posted on 2004-04-08
6
178 Views
Last Modified: 2012-05-04
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






0
Comment
Question by:ISGDude
  • 5
6 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10785579


 if TLabel(Sender).Caption = '0' then

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10785607

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
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10785648
hmmmm, wht the 'B' grade was that not what you wanted?

Shane
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10785667
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  
0
 

Author Comment

by:ISGDude
ID: 10785780
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?


0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10785874
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question