Solved

Reusable Click routines

Posted on 2004-04-08
6
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

623 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