Focus next control on enter - in overridden KeyUp

喜夏-厌秋 提交于 2020-01-20 03:24:25

问题


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?


回答1:


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



回答2:


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;



回答3:


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;



回答4:


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.



来源:https://stackoverflow.com/questions/6773400/focus-next-control-on-enter-in-overridden-keyup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!