Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - Focus next control on enter - in overridden KeyUp

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...