Solved

Reusable Click routines

Posted on 2004-04-08
6
179 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 104
Internet Explorer View Settings Question 15 122
When i run adoquery my application freezes 26 188
scroll down TListBox component in Delphi 1 30
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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