KeyDown event handler not working

Why am I getting this error?

Declaration of 'RichEdit3KeyDown' differs from previous declaration
and
Unsatisfied forward or external declaration 'TForm1.RichEdit3KeyDown

Its weird, all im trying to is put code in the RIchEdit3Keydown method handler

One thing I have noticed though is the procedure is always wordwrapped

so instead of

procedure TForm1.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

it appears like this instead

procedure TForm1.Edit28KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);

might that make a difference?

Ben
Alpha_AIAsked:
Who is Participating?
 
rfwoolfConnect With a Mentor Commented:
I think I know what happened...

If you declare it under "type" then you have to refer to it as TForm1.Edit28KeyDown, BUT, if you declare it under "var" then you can drop "TForm1" and just used Edit28KeyDown.

See the following I've typed up:

Your procedure declaration needs to come in the interface section of the form's unit (that's before the "implementation" section).
Then, if you put it down under 'type' as a type declaration it will be owned by the form, as in TForm1.MyProcedure
If however you put it under 'var' it will become a global procedure, as in just MyProcedure.
However, even though it may be "global", to use it from other units you would still have to put the unit in the other forms uses clause. e.g. If your procedure is in Unit 1, you would add Unit 1 to Unit 2's uses clause in order to use it.

---

About your problem, you need to go to the problem form, and do a search for the problem procedure - do this using Ctrl + F or using the menus. You will then have to take note of that procedure's declarations.

If you wish to make it global, declare it under the 'var' section just before 'implementation', and don't add the TForm1 to it, as follows. Declare it as follows:
procedure Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

And once again, that header must be repeated exactly in your implementation section, as follows:

procedure Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
 ...
end;

---
HOPE THIS HELPS
0
 
TheRealLokiSenior DeveloperCommented:
no, that will make no difference

you will have a typo somewhere, that is all
Check the header declaration
(CTRL-SHIFT-UP or CTRL-SHIFT-DOWN to go between declaration and method)
and cut and paste the correct one

It may be because you had it declared as
RichEdit3KeyDown
but you know say Edit28KeyDown
0
 
Alpha_AIAuthor Commented:
that was just an example

the method declaration is

procedure TForm1.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

and it appears like this in the code

procedure TForm1.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

Its weird because I clear the code in the method handler and compile it doesnt compile
when i remove the component altogether it compiles
when i put the component editbox back on
click the keydown in events
and put a comment inside the code
and then compile it doesnt work. It comes up with that error

The KeyDown doesnt work but the KeyPress does..

Ben
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Alpha_AIAuthor Commented:
the method declaration is:

    procedure Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

and it appears like this in the code

procedure TForm1.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

looks like it should work but it comes up with the error and when i remove the editbox it works fine.

Ben
0
 
Alpha_AIAuthor Commented:
but i tried a new application with just a editbox and then it works.
So is there a quick way to find the error in the original code.

some form of analysing technique included in Delphi 2007?

Ben
0
 
TheRealLokiSenior DeveloperCommented:
make sure there is not already another function called Edit28KeyDown.

Just as a test, while it compiles (ie. your component is removed)
put a new control on the form with a different name.
create the header declaration and method manually, then hook up the event in the object inspector maually.
does that work?
0
 
ziolkoCommented:
check your uses clausule, maybe you have TShiftSate defined in two different units

ziolko.
0
 
ziolkoCommented:
...like

interfcace
uses unit1
...
.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState <- defined in unit1);

implementation
uses unit2

.Edit28KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState <- defined in unit2);

ziolko.
0
 
bogdan_iulyCommented:
Hy!

Check to see if Delphi didn't get confused and end up by introducing the code between E and N from END. It happens very often in my D7. :)

Also, check the Object Inspector OnKeyDown value.

The declaration of the event should be before the private area. Check that too.

Or post the code here so that we can check it.

Here is the code for a basic application with a RichEdit, a OnKeyDown event handler.
---------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
//Do something
end;

end.

----------------------------------

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object RichEdit1: TRichEdit
    Left = 208
    Top = 128
    Width = 185
    Height = 89
    Lines.Strings = (
      'RichEdit1')
    TabOrder = 0
    OnKeyDown = RichEdit1KeyDown
  end
end
0
 
diniludConnect With a Mentor Commented:
Right click on TShiftState. Click on find target.
The declaration must be in "classes" unit.
0
 
ziolkoCommented:
any progress?

ziolko.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.