Solved

Common procedure for controls

Posted on 2009-05-15
9
185 Views
Last Modified: 2012-05-07
Hi Experts,

I have on a form a lot of editboxes (nearly 40). Each of them contains a short text. When the user clicks the editbox this editbox will be empty.

Type ebxName: TEdit;

procedure TfrmNew.ebxNameClick(Sender: TObject);
begin
   ebxName.Text:='';
end;

But I 'd like to have a common procedure for each editbox instead of the ~40 procedures.
Thanks for your support!
Janos
0
Comment
Question by:kacor
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 24399559
If you click on an edit box, and go into the object inspector, and click on the events tab, next to OnClick - you can put any valid procedure in there.

For example:

procedure TForm1.ClearEditBox(Sender : TObject);
begin
  if Sender is TEdit then
     Sender.Text := '';
end;

Then select all your edit boxes, go to events tab, under Onclick paste in 'ClearEditBox'

Hopefully that will solve your problem
0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 167 total points
ID: 24399563
No problem.
You can use the Sender parameter to handle the problem for you.
I think I have the code correct in the snippet I am providing.   (I am in the middle of working on a commandline app and don't have the code where I do this sort of thing handy. ;-)
Just change the various OnClick events to all point to the one "ClearTextBoxClick" evenet handler.

procedure TfrmNew.ClearTextBoxClick(Sender: TObject);
begin
   TEdit(Sender).Text:='';
end;

Open in new window

0
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 166 total points
ID: 24399571
you can use the same event handler for all of your edit boxes
procedure TfrmNew.ebxNameClick(Sender: TObject);
begin
   TEdit(Sender).Text := '';
end;

Open in new window

0
Industry Leaders: 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 14

Expert Comment

by:SteveBay
ID: 24399573
Too late :)
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24399590
Yup . . . by about 1 minute! ;-)
0
 
LVL 13

Assisted Solution

by:rfwoolf
rfwoolf earned 167 total points
ID: 24399617
Haha... I got it for once.
But I think 8080's (and SteveBay's) code is better - I didn't test mine, so go with this:
 if Sender is TEdit then
     TEdit(Sender).Text := '';
0
 
LVL 10

Author Closing Comment

by:kacor
ID: 31582098
Hi guys! Thanks for your quick help! Each solution is perfect!
Janos
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24399862
rfwoolf,
Yours does have the advantage of making sure that the Send is a TEdit . . . however, since the developer has control over which cotrnols are calling the event handler, it would take a rather dimwitted developer to connect, for instance, a TRadioButton to the event handler (especially if it is named something like 'ClearTextBoxClick') . . . however, I have worked at places where I coded like that to prevent my co-workers from doing such silly things. ;-)
 
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 24400164
8080> LOL.  I hear you. However as I continue to grow as a developer, and as I further master object orientated programming, the philosophy seems to be to do stupid things like that. Just as you anticipate a user doing stupid things like entering letters when you ask for numbers or visa verse (you can of course use a TJvValidateEdit) or if it asks for whole numbers they can put fractions or decimals or if they leave something blank, or if they put a negative number  etc. -- so too should we consider that developers might use this procedure incorrectly.
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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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