Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OnKeyPress in inherited component

Posted on 1999-07-07
13
Medium Priority
?
757 Views
Last Modified: 2010-04-06
I have derived a component from TSpinEdit and I have written my own OnKeyPress event assigning it in the construstor. I have had stack errors (recusrion problem) calling the inherited KeyPress. How should I deal with calling the inherited event?

Thanks a lot.

Tom.
0
Comment
Question by:tomcorcoran
[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
13 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386964
Did you use 'inherited' in your OnKeyPress event?

TYourComponent = class(...)
    procedure KeyPress(var Key: Char); override;


TYourComponent.KeyPress(var Key: Char);
begin
  inherited KeyPress(Key);
  // your code
end;

0
 

Author Comment

by:tomcorcoran
ID: 1386965
I did that and like I say it goes all recursive, setting off Thanks, Tom. a stack error.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386966
TSpinEdit must do something weird then. Unfortunatly I have no sources of it.
Does the override actually work?
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!

 

Expert Comment

by:anour
ID: 1386967
Hello

Do you call "inherited KeyPress ..." from within OnKeyPress event?

OnKeyPress is a TWinControl event that is called by TWinControl.KeyPress method. When you make call to the inherited function, it calls the OnKeyPress event, that call the inherited function, and it calls the event again which calls the inherited function .... (you get the picture?)

That's the reason for your overflows. There is no need to call "inherited ...", just write your handler.

HTH

Ali
0
 

Accepted Solution

by:
Byteback earned 200 total points
ID: 1386968
The reason for the stach overflow is as anour said.
When building custom components you should not assign your own event handler from the constructor but override the function which calls the event handler.

In the example below '0' and '1' are processed normally. '9' acts like a '2' and all other keys are ignored.

TOwnSpinEdit = class(TSpinEdit)
  protected
    procedure KeyPress(var Key: char): override;
end;

procedure TOwnSpinEdit.KeyPress(var Key: char);
begin
  if key in ('0', '1') then
  begin
    // process key normally
    inherited;
  end;
  if key = '9' then
  begin
    // change key, then process
    key = '2';
    inherited;
  end;
end;

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386969
Hey, I already said that!
0
 

Author Comment

by:tomcorcoran
ID: 1386970
Thanks dude, that clears things up. Tom.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386971
Tom, next time read my comment better!!!!!!
0
 

Author Comment

by:tomcorcoran
ID: 1386972
Eps, it was a close call, I felt the dude privided a clearer explantion and he also noted that the inherited came after the code and not before as you wrote :-( Thanks, Tom.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386973
The order completely depends on what you want to do.
0
 

Author Comment

by:tomcorcoran
ID: 1386974
now I am confused totally, is there somewhere I can read up on this? When should one call after and when show one call before? Thanks and apologies for the assumptions. Tom.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386975
If you want to prevent some keys being passed to TSpinEdit then use something like Byteback did. But if you just want to check which key a use typed (e.g. Beep on a returnkey press) it does not realy matter what comes first.


'inherited' means that the original KeyPress of TSpinEdit is executed.


  inherited KeyPress(var Key: char); // full statement

does exactly the same as

  inherited; // short version


That's it!

Epsylon.
0
 

Author Comment

by:tomcorcoran
ID: 1386976
thanks epsylon, I am posting a dummy question on keypress, answer and refer to this and I will grade, Thanks, Tom.
0

Featured Post

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!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 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