Solved

Reusable Click routines

Posted on 2004-04-08
6
175 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now