Link to home
Start Free TrialLog in
Avatar of APS NZ
APS NZFlag for New Zealand

asked on

Using Enter instead of Tab in edit controls.

I have just started using D4 - a leap from D1.  I want to make edit controls respond to the Enter key instead of Tab to move between controls on a form.  In D1 I used the following code:

procedure TEnterEdit.KeyPress(var Key: Char);
var
   MYForm: TForm;
begin

   if Key = #13 then
   begin
       MYForm := GetParentForm( Self );
       if not (MYForm = nil ) then
           SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
       Key := #0;
   end;

   if Key <> #0 then inherited KeyPress(Key);

end;

When I tried to compile the component under D4 I got an error telling me that I couldn't use the MyForm variable because of a difference with TCustomForm.

What do I need to change to get the component to work under D4?
Avatar of inthe
inthe

this page:
http://www.inprise.com/delphi/news/zd/1998/oct98/

gives you the code from borland about using enter as tab,maybe of some help to you but im not sure
Regards Barry
Or you can use this:

procedure SimulateKey(lhwnd: HWND; key: char);
var repeatcount, scancode, contextcode, previouskeystate, transitionstate, lparam: integer;
begin
  repeatcount:=1; scancode:=VkKeyScan(key); contextcode:=0;
  previouskeystate:=0; transitionstate:=0;
  lparam:=repeatcount or (scancode shl 16) or (contextcode shl 29) or
      (previouskeystate shl 30) or (transitionstate shl 31);
  PostMessage(lhwnd, WM_KEYDOWN, ord(key), lparam);
  previouskeystate:=1; transitionstate:=1;
  lparam:=repeatcount or (scancode shl 16) or (contextcode shl 29) or
      (previouskeystate shl 30) or (transitionstate shl 31);
  PostMessage(lhwnd, WM_KEYUP, ord(key), lparam);
end;

procedure TEnterEdit.KeyPress(var Key: Char);
begin
  if Key=#13 then begin
    Key:=#0;
    SimulateKey(handle,#9);
  end else inherited;
end;

Regards, Madshi.
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh I didn;t see it was for a component.. Ok, here is how it would be for a component...

   procedure TEnterEdit.KeyPress(var Key: Char);
   begin
      if key = #13 then
              Application.MainForm.Perform(WM_NEXTDLGCTL, 0, 0);
   end;

That's all :))
Avatar of APS NZ

ASKER

Hi to Madshi, Inthe, and Viktornet

Tahnks a lot for the help.