Jump to content

Making stringgrid component

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
rqdo5

rqdo5

    Newbie

  • Members
  • Pip
  • 2 posts
Hi
I am trying to make new stringgrid component, where i can only type numbers and i've got something like this:
procedure TStringGrid1.KeyPress(Sender: TObject; var Key: Char);

var

  MyKeyDown : TKeyPressEvent;

begin

    MyKeyDown := OnKeyPress;

    if Assigned(MyKeyDown) then begin if Key in ['0'..'9',#8, '-'] then Key := Key

else Key := #0; MyKeyDown(Self, Key); end;

end;
of course it doesn't work, so what wrong is in this code? (compiler is not showing any errors.)

Edited by rqdo5, 18 December 2010 - 05:16 PM.


#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
I haven't checked it yet, but I think the reason it doesn't work is because you did not call inherited KeyPress anywhere in that overriding method (it is an override, right?). Instead of calling the OnKeyPress event with MyKeyDown(Self, Key) try replace it with inherited;, and don't forget to add begin right afrer the else.

Let me know if this solves the problem (or not).

#3
rqdo5

rqdo5

    Newbie

  • Members
  • Pip
  • 2 posts
I add "override" and now it's working.
btw. second way with "inherited" does not work.

Thanks for reply.

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts

rqdo5 said:

I add "override" and now it's working.

Glad to know that. So the real problem was you had "erased" KeyPress method from the parent (in which OnKeyPress event is triggered) instead of override it.

rqdo5 said:

btw. second way with "inherited" does not work.

Since you had "erased" KeyPress method (by introducing new method with the same name, since you did not use override) from the parent, inherited would never work, since there was nothing was inherited . :)