Board index » delphi » A more generic events

A more generic events


2003-11-26 12:28:29 AM
delphi44
Hi guys, I figure that this is simple enough for you guys.
I have a similar action which I'd like to perform on some edit boxes
upon exiting. Basically checking to see if they are numeric.
Rather than creating code to check each separate box, how would I go
about using the Sender as the object and coding the event to it. So
that I wouldn't have to either write a separate event for each box or
check to see which box was focused.
Thanks
Mike
 
 

Re:A more generic events

"Mike" <XXXX@XXXXX.COM>writes XXXX@XXXXX.COM...
Quote
Rather than creating code to check each separate box, how would I go
about using the Sender as the object and coding the event to it. So
that I wouldn't have to either write a separate event for each box or
check to see which box was focused.
if Sender is TCustomEdit then
begin
TCustomEdit(Sender)....
...
end;
Cheers,
Ignacio
 

Re:A more generic events

Mike writes:
Quote
Hi guys, I figure that this is simple enough for you guys.
Gee, I hope so, too.
Quote
I have a similar action which I'd like to perform on some edit boxes
upon exiting. Basically checking to see if they are numeric.
I think most people go the route of preventing non-numeric input in the
first place. See the article below for information on that.
groups.google.com/groups
Quote
Rather than creating code to check each separate box, how would I go
about using the Sender as the object and coding the event to it. So
that I wouldn't have to either write a separate event for each box or
check to see which box was focused.
The Sender parameter refers to the object that triggered the event. For
Edit1.OnExit, Sender will refer to Edit1. For Edit2.OnExit, Edit2. It's
that simple. Since Sender is usually of type TObject, you will need to
type cast it to the type you need: TEdit(Sender).
You may be tempted to display a message box or change the focus if the
OnExit event determines that the contents of the edit box aren't
numeric. Resist that temptation. It is not a good idea to change the
input focus while handling a focus-changing event. Instead, post a
message (using PostMessage) to your form and display the dialog box or
change the focus when handling that message. The focus-changing message
is guaranteed to be finished by that time. Be careful that you don't get
stuck in a loop, jumping between two non-numeric edit boxes.
--
Rob
 

Re:A more generic events

Ignacio Vazquez writes:
Quote
"Mike" <XXXX@XXXXX.COM>writes XXXX@XXXXX.COM...
>Rather than creating code to check each separate box, how would I go
>about using the Sender as the object and coding the event to it. So
>that I wouldn't have to either write a separate event for each box or
>check to see which box was focused.

if Sender is TCustomEdit then
begin
TCustomEdit(Sender)....
...
end;

Cheers,
Ignacio
Thanks
 

Re:A more generic events

Rob Kennedy <.>writes:
Quote
Mike writes:
>Hi guys, I figure that this is simple enough for you guys.

Gee, I hope so, too.
Mike's confidence in us is overwhelming, isn't it? :)
So, Mike, where do you send your tough questions?
Rick Carter
XXXX@XXXXX.COM
Chair, Paradox/Delphi SIG, Cincinnati PC Users Group
 

Re:A more generic events

In article <XXXX@XXXXX.COM>, XXXX@XXXXX.COM says...
Hi,
Quote
I have a similar action which I'd like to perform on some edit boxes
upon exiting. Basically checking to see if they are numeric.

Rather than creating code to check each separate box, how would I go
about using the Sender as the object and coding the event to it.
I would not use events, I'd create a TEdit descendant with your
checks in it, and use those instead
Phil
 

Re:A more generic events

Rob Kennedy writes:
Quote
Mike writes:
>Hi guys, I figure that this is simple enough for you guys.

Gee, I hope so, too.

>I have a similar action which I'd like to perform on some edit boxes
>upon exiting. Basically checking to see if they are numeric.

I think most people go the route of preventing non-numeric input in the
first place. See the article below for information on that.

groups.google.com/groups

>Rather than creating code to check each separate box, how would I go
>about using the Sender as the object and coding the event to it. So
>that I wouldn't have to either write a separate event for each box or
>check to see which box was focused.

The Sender parameter refers to the object that triggered the event. For
Edit1.OnExit, Sender will refer to Edit1. For Edit2.OnExit, Edit2. It's
that simple. Since Sender is usually of type TObject, you will need to
type cast it to the type you need: TEdit(Sender).

You may be tempted to display a message box or change the focus if the
OnExit event determines that the contents of the edit box aren't
numeric. Resist that temptation. It is not a good idea to change the
input focus while handling a focus-changing event. Instead, post a
message (using PostMessage) to your form and display the dialog box or
change the focus when handling that message. The focus-changing message
is guaranteed to be finished by that time. Be careful that you don't get
stuck in a loop, jumping between two non-numeric edit boxes.

--
Rob
Went through the creating a component thingie and it works well. Going to play around
with it a bit and learn to allow to check for real numbers and such. I hadn't built a
component before, was a little intimidated by that idea but it was relatively painless
reading through the article.
Thanks for all the help
Mike