Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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?
0
jdthedj
Asked:
jdthedj
1 Solution
 
intheCommented:
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
0
 
MadshiCommented:
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.
0
 
viktornetCommented:
Here is how to do it...

if key = #13 then
  Perform(WM_NEXTDLGCTL, 0, 0);

That;s all...no need for anything else...

-Viktor
--Ivanov
0
 
viktornetCommented:
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 :))
0
 
jdthedjAuthor Commented:
Hi to Madshi, Inthe, and Viktornet

Tahnks a lot for the help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now