Question

In one form of my application, we add sets of data by adding frames to the form. For each frame, we want to be able to move from one edit (Dev Express Editors) control to the next by pressing the Enter key. So far, I have tried four different methods in my control's KeyPress and KeyUp events.

  1. SelectNext(TcxCurrencyEdit(Sender), True, True); // also base types attempted

  2. SelectNext(Sender as TWinControl, True, True);

  3. Perform(WM_NEXTDLGCTL, 0, 0);

  4. f := TForm(self.Parent); // f is TForm or my form c := f.FindNextControl(f.ActiveControl, true, true, false); // c is TWinControl or TcxCurrencyEdit if assigned(c) then c.SetFocus;

None of these methods are working in Delphi 5. Can anyone guide me towards getting this working? Thanks.

Was it helpful?

Solution

This works in Delphi 3, 5 and 6:

Set form's KeyPreview property to True.

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  If (Key = #13) then
  Begin
    SelectNext(ActiveControl as TWinControl, True, True);
    Key := #0; 
  End;
end;

OTHER TIPS

I found one old project that catches CM_DIALOGKEY message when user presses Enter key and then it fires VK_TAB key . It works with number of different controls.

interface
... 
  procedure CMDialogKey(var Message: TCMDialogKey);message CM_DIALOGKEY;

implementation
...

procedure TSomeForm.CMDialogKey(var Message : TCMDialogKey);
begin
  case Message.CharCode of
    VK_RETURN : Perform(CM_DialogKey, VK_TAB, 0);
    ...
  else
    inherited;
  end;
end;

The event onKeyPress is trigered like any other form.

The problem is that the procedure perform(wm_nextdlgctl,0,0) doen't work inside the frame.

You must know the active control to triger the proper event.

procedure TFrmDadosCliente.EditKeyPress(Sender: TObject; var Key: Char);
var
  AParent:TComponent;
begin
  if key = #13 then
  begin
    key := #0;

    AParent:= TComponent(Sender).GetParentComponent;

    while not (AParent is TCustomForm) do
      AParent:= AParent.GetParentComponent;

    SelectNext(TCustomForm(AParent).ActiveControl, true, true);
  end;
end;

You can place a TButton on the form, make it small and hide it under some other control. Set the Default property to true (that makes it getting the Enter key) and place the following into the OnClick event:

SelectNext(ActiveControl, true, true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top