gpt4 book ai didi

DELPHI Edit.OnExit 通过 TAB,在焦点上显示窗口结果 bug

转载 作者:行者123 更新时间:2023-12-02 10:26:10 25 4
gpt4 key购买 nike

我在遇到以下情况时遇到问题:

  • 2 编辑
  • 在 Edit1 中输入内容并按 TAB 键,焦点将转到 Edit2
  • Edit1.OnExit -> 显示带有消息“正在处理...”的表单(进行冗长的验证)

表单关闭后,Edit2上的焦点似乎“崩溃”了......- 未选择 Edit2 中的孔文本- 插入符不闪烁

示例:

  • 创建新表单
  • 进行 2 项修改
  • 在 Edit1 中将其设置为 OnExit 事件:

    procedure TForm1.Edit1Exit(Sender: TObject);
    begin
    with TForm.CreateNew(self) do
    try
    Width := 100;
    Height := 50;
    Position := poMainFormCenter;
    show;
    sleep(200);
    finally
    Free;
    end;
    end;
  • 运行应用程序

  • 将焦点置于 Edit1 中并按 TAB

我正在使用:

  • Delphi 7 企业版
  • Windows 7 x64

最佳答案

这是一个已知问题。当您在完成最后一次焦点更改之前更改焦点时,Windows 会出现问题(例如,焦点开始从 Edit1 更改为 Edit2,但 Edit1.OnExit) > 执行某些操作将焦点更改为另一个控件或表单。

例如,当应用尝试在 OnExit 事件中进行验证,然后在验证失败时尝试将焦点返回到原始控件时,就会发生这种情况。

最简单的解决方案是在 OnExit 中向表单句柄发布消息,并在那里处理焦点更改需求。一旦目标控件获得输入焦点,它将触发,并且 Windows 不会感到困惑。

const
UM_EDIT1_EXITED = WM_USER + 1;

type
TForm1=class(TForm)
...
private
procedure UMEdit1Exited(var Msg: TMessage); message UM_EDIT1_EXITED;
end;

implementation

procedure TForm1.Edit1Exit(Sender: TObject);
begin
PostMessage(Handle, UM_EDIT1_EXITED, 0, 0);
end;

procedure TForm1.UMEdit1Exited(var Msg: TMessage);
begin
// Show your other form here
end;

摘自 Peter Below 博士的旧 Borland NG 帖子 TeamB :

here is my general sermon on the "show dialog from OnExit" problem:

If an OnExit handler is triggered (which happens in response to the Windows message WM_KILLFOCUS) Windows is in the midst of a focus change. If you do something in the handler that causes another focus change (like popping up a message box or doing a SetFocus call) Windows gets terribly confused. The missing cursor is a symptom of that.

If you have to display a message to your user from an OnExit handler, do it this way:

  1. Define a constant for a user message somewhere in the INterface section of your unit, above the type declaration for your form

    'Const UM_VALIDATE = WM_USER + 200;'

  2. Give your Form a handler for this message, best placed in the private section of the class declaration:

    Procedure UMValidate( Var Msg: TMessage ); message UM_VALIDATE;

  3. Post a UM_VALIDATE message to the form from the OnExit handler if the contents of the field are not ok. You can pass additional information in the wparam and lparam parameters of the message, e.g. an error number and the Sender object. In fact you could do the whole validation in the UMValidate handler!

关于DELPHI Edit.OnExit 通过 TAB,在焦点上显示窗口结果 bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11155718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com