将下一个控件聚焦在 Enter 上 - 在覆盖的 KeyUp 中

问题描述 投票:0回答:4

我有扩展 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;

在 KeyUp 过程中我做:

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;

但它并没有将焦点转移到下一个控件。我尝试过

if Key = VK_RETURN then
      Key := VK_TAB;

但它也不起作用。我错过了什么?

delphi delphi-2010
4个回答
15
投票

SelectNext
选择下一个同级子控件,即。您需要在编辑的父级上调用它:

type
  THackWinControl = class(TWinControl);

if Key = VK_RETURN then
  if Assigned(Parent) then
    THackWinControl(Parent).SelectNext(Self, True, False);

6
投票

这里是 PostMessage 方法(使用 Messages)以供记录:)

procedure TMyEdit.KeyUp(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if FocusNextOnEnter then
    if Key = VK_RETURN then begin
      PostMessage(GetParentForm(Self).Handle, wm_NextDlgCtl, Ord((ssShift in Shift)), 0);
      Key := 0;
    end;
end;

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;

0
投票

THackWinControl 可以避免,并且让它变得更好:

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

问题是它仍然下拉组合框选项。

我正在努力。

© www.soinside.com 2019 - 2024. All rights reserved.