如何在框架内移动到下一个控件?

3
在我的应用程序的一个形式中,我们通过向表单添加框架来添加一组数据。对于每个框架,我们希望能够通过按Enter键从一个编辑(Dev Express编辑器)控件移动到下一个。到目前为止,在控件的KeyPress和KeyUp事件中,我已尝试了四种不同的方法。
1. SelectNext(TcxCurrencyEdit(Sender), True, True); //也尝试了基本类型 2. SelectNext(Sender as TWinControl, True, True); 3. Perform(WM_NEXTDLGCTL, 0, 0); 4. f := TForm(self.Parent); //f是TForm或我的表单 c := f.FindNextControl(f.ActiveControl, true, true, false); //c是TWinControl或TcxCurrencyEdit 如果分配了c,则设置焦点。
在Delphi 5中,这些方法均无效。谁能指导我使其正常工作?谢谢。
4个回答

4

这在 Delphi 3、5 和 6 中都适用:

将窗体的 KeyPreview 属性设置为 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;

不确定为什么将其提升到表单级别时它能起作用,但我猜测这与帧的有限交互能力有关。不过效果非常好,谢谢。 - Tom A

3

我发现一个旧项目,当用户按下回车键时,它捕获CM_DIALOGKEY消息,然后触发VK_TAB键。它适用于多种不同的控件。

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;

在低级别的更多组件中运作得更好。 - user2092868

2

事件onKeyPress像任何其他表单一样被触发。

问题在于过程perform(wm_nextdlgctl,0,0)在框架内部不起作用。

你必须知道活动控件才能触发适当的事件。

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;

1

您可以在表单上放置一个TButton,将其缩小并隐藏在其他控件下面。将Default属性设置为true(这使它获得Enter键),并将以下内容放入OnClick事件中:

SelectNext(ActiveControl, true, true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接