13

I have my custom class that extends TEdit:

  TMyTextEdit = class (TEdit)
   private
     fFocusNextOnEnter: Boolean;
   public
    procedure KeyUp(var Key: Word; Shift :TShiftState); override;
   published
     property FocusNextOnExnter: Boolean read fFocusNextOnEnter
                                 write fFocusNextOnEnter default false;
  end;

In The KeyUp procedure I do:

procedure TMyTextEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
  inherited;

  if FocusNextOnExnter then
    if Key = VK_RETURN then 
      SelectNext(Self as TWinControl, True, false);
end;

But it isn't moving focus to the next control. I tried to

if Key = VK_RETURN then
      Key := VK_TAB;

but it isn't working either. What am I missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JustMe
  • 2,329
  • 3
  • 23
  • 43

4 Answers4

14

SelectNext selects next sibling child control, ie. you need to call it on your edit's parent:

type
  THackWinControl = class(TWinControl);

if Key = VK_RETURN then
  if Assigned(Parent) then
    THackWinControl(Parent).SelectNext(Self, True, False);
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Thanks, but it is not working like a tab key - it always move focus to a first control on a form – JustMe Jul 21 '11 at 09:22
  • 2
    It should move to the next control based on the `TabOrder` and `TabStop` properties of other sibling controls. – Ondrej Kelle Jul 21 '11 at 09:23
  • 2
    @JustMe, I just tested TOndrej's code (because I was going to post the same thing, but he was faster). It works just fine, I've got 3 `TYourTextEdit`'s on my form and focus circulates from one to the other, just as you'd expect. – Cosmin Prund Jul 21 '11 at 09:26
  • Try passing `True` as the last parameter (`CheckTabStop`). – Ondrej Kelle Jul 21 '11 at 09:29
  • 4
    What if the parent is one container of many on the form. Would this code just cycle around the controls in that container or would it reach out to the first control of the next container? – David Heffernan Jul 21 '11 at 09:30
  • @Tondrej I'm sorry, it's working like expected, must have some junk from old project :) – JustMe Jul 21 '11 at 09:30
  • @David To be able to jump to controls in a different container you have to replace both occurrences of `Parent` with `GetParentForm(Self)`. – Sebastian Kirsche Nov 02 '11 at 12:35
6

Here's the PostMessage approach (uses Messages) for the record :)

procedure TMyEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if FocusNextOnExnter then
    if Key = VK_RETURN then begin
      PostMessage(GetParentForm(Self).Handle, wm_NextDlgCtl, Ord((ssShift in Shift)), 0);
      Key := 0;
    end;
end;
JustMe
  • 2,329
  • 3
  • 23
  • 43
5
procedure TMyEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin

  inherited;

  if FocusNextOnExnter and Focused and (Key = VK_RETURN) then 

  begin

    Perform(CM_DIALOGKEY, VK_TAB, 0);

    Key := 0;

  end;

end;
Trinimon
  • 13,839
  • 9
  • 44
  • 60
Tacu Aureliu
  • 51
  • 1
  • 1
0

The THackWinControl can be avoided, and to make it nice:

SelectNext(ActiveControl as TWinControl, True, ssShift in Shift);

Problem is it still pull down combobox choices.

I'm working on that.

k9dog
  • 31
  • 4