Solved

Common procedure for controls

Posted on 2009-05-15
9
184 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
  • 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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