Board index » delphi » Invalid typecast!!!

Invalid typecast!!!

(((Sender as TControl).Parent) as TDBCtrlGrid).DoKey(gkScrollDown);

This compiles but does not run, error - Invalid Typecast
Any ideas?

It is part of a KEY-DOWN event in a DBCtrlGrid.

Kind Regards,
Bruce

 

Re:Invalid typecast!!!


Quote
Bruce Swart wrote:

> (((Sender as TControl).Parent) as TDBCtrlGrid).DoKey(gkScrollDown);

> This compiles but does not run, error - Invalid Typecast
> Any ideas?

> It is part of a KEY-DOWN event in a DBCtrlGrid.

> Kind Regards,
> Bruce

How about using TWinControl instead of TControl.

(((Sender as TWinControl).Parent) as TDBCtrlGrid).DoKey(gkScrollDown);

If I am not mistaken, the TDBCtrlGrid component has a window handle.

HTH

Rkr

--

                   \|||/
                   /'^'\
                  ( 0 0 )
--------------oOOO--(_)--OOOo--------------
. Reid Roman                              .
. Programmer / Analyst                    .
. TVisualBasic := Class(None)             .
. May the Source be With You              .
-------------------------------------------
. Auto-By-Tel (http://www.autobytel.com)  .
. Irvine, CA U.S.A                        .
. E-Mail : rkroman (at) home (dot) com    .
-------------------------------------------

Re:Invalid typecast!!!


Quote
Bruce Swart wrote:

> (((Sender as TControl).Parent) as TDBCtrlGrid).DoKey(gkScrollDown);

> This compiles but does not run, error - Invalid Typecast
> Any ideas?

> It is part of a KEY-DOWN event in a DBCtrlGrid.

So if it's an event of a DBCtrlGrid why are you getting it's parent?
Maybe

   (((Sender as TDBCtrlGrid).DoKey(gkScrollDown);

will work.

Jochen

Re:Invalid typecast!!!


Hi,

I agree with the message Jochen wrote, however, it is always a good idea to
test sender for its class before typecasting it this will prevent {*word*193}
Invalid type cast errors:

if (Sender is TDBCtrlGrid) then
  (Sender as TDBCtrlGrid).DoKey(gkScrollDown);

Hope this helps,

Earl Reddell
Res-cue (Resourceful Components for User Ease!)
Check out our web site: http://www.res-cue.com

Quote
> (((Sender as TControl).Parent) as TDBCtrlGrid).DoKey(gkScrollDown);

> This compiles but does not run, error - Invalid Typecast
> Any ideas?

> It is part of a KEY-DOWN event in a DBCtrlGrid.

> Kind Regards,
> Bruce

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Invalid typecast!!!


In this case, the sender is a TDBEdit, whose
parent is the TDBCtrlGrid.

Thanks for the try though. :)

Re:Invalid typecast!!!


Quote
In article <7g3gkg$tp...@nnrp1.dejanews.com>, eredd...@my-dejanews.com writes:
>if (Sender is TDBCtrlGrid) then
>  (Sender as TDBCtrlGrid).DoKey(gkScrollDown);

If you're using an "if" construction to test for a TDBCtrlGrid before
type-casting then you don't need to use the protected type-cast (the variable
will always be castable to a TDBCtrlGrid), just use :-

if (Sender is TDBCtrlGrid) then
  TDBCtrlGrid(Sender).DoKey(gkScrollDown);

Alan Lloyd
alangll...@aol.com

Other Threads