T编辑焦点和插入符

发布于 2024-12-02 23:41:02 字数 407 浏览 0 评论 0原文

我有两个 TEdit 控件。当我退出 edit1 时, edit2 获得焦点。在我的 Edit1 的 OnExit 事件上 我有以下代码:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
end;

Edit2 具有焦点。但是,其中没有插入符号。 我可以开始打字,但很混乱,因为我不知道 哪个控件具有焦点。

我更感兴趣的是翻转的效果 导致某些消息不可用的已启用属性 正确射击?例如edit2的OnEnter事件 没有被触发。

如果有什么关系的话,那就是《D2006》了。

感谢您的回复。

I got two TEdit controls. When I tab out of edit1,
edit2 receives the focus. On my OnExit event of Edit1
I have the following code:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
end;

Edit2 has the focus. However, there is no caret in it.
I can start typing but it's confusing as I do not know
which control has the focus.

I'm more interested on what's with the flipping of the
Enabled property that's causing some messages to be not
firing properly ? For instance edit2's OnEnter event
is not being triggered.

This is on D2006 if it matters at all.

Thanks for the reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

另类 2024-12-09 23:41:02

我不明白为什么您禁用并启用 edit2,但您这样做:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
  PostMessage(edit2.Handle, WM_SETFOCUS, 0, 0);
end;

顺便说一句,我同意 Andreas Rejbrand 的观点。

I don't understand why you disable and enable edit2, but you do this:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  edit2.Enabled := false;
  edit2.Enabled := true;
  edit2.setfocus;
  PostMessage(edit2.Handle, WM_SETFOCUS, 0, 0);
end;

BTW, I agree with Andreas Rejbrand.

茶花眉 2024-12-09 23:41:02

我严重怀疑你正在以错误的方式做某事,最好的解决方案很可能是重新设计。您不应该在控件接收焦点时禁用然后启用该控件。

I seriously suspect you are doing something in a bad way, and the best solution is most likely a redesign. You are not supposed to disable and then enable a control while it is receiving focus.

七度光 2024-12-09 23:41:02

禁用和启用 edit2 之间有一堆代码。

在前一个活动控件的 OnExit 事件处理程序中包含大量代码不需要需要禁用下一个活动控件。该代码在下一个活动控件显示插入符号之前执行,并且能够接收用户输入。只需确保它不会通过启动新线程或使用 Application.ProcessMessages 之类的方式传递执行即可。

Screen.Cursor 设置为 crHourGlass 以使用户清楚地知道下一个活动控件尚未准备好。

There's a bunch of codes between disabling and enabling edit2.

Having a lot of code in the OnExit event handler of the previous active control does not require to disable the next active control. That code will execute before the next active control shows up the caret and will be able to receive user input. Just make sure that it does nót pass execution over by something like starting a new thread or using Application.ProcessMessages.

Set Screen.Cursor to crHourGlass to make it clear for the user that the next active control is not ready yet.

快乐很简单 2024-12-09 23:41:02

当 MainForm 的 OnActive 激活另一个表单时发现问题。

TMainForm.OnActivate;
begin
ChildForm.ShowModal;
end;

控制焦点已设置但不起作用。我发现的解决方法是发送 PostMessage(Handle, WM_SETFOCUS, 0, 0);到表单句柄。

procedure TChildForm.FocusControl(AWinControl: TWinControl);
begin
  try
    // http://stackoverflow.com/questions/7305296/tedit-focus-caret
    PostMessage(Handle, WM_SETFOCUS, 0, 0);
    PostMessage(AWinControl.Handle, WM_SETFOCUS, 0, 0); 
    if AWinControl.CanFocus then
       AWinControl.SetFocus;
  except
    on E: Exception do
    begin
      Error(Self, E);
    end;
  end;
end;

Found an issue when OnActive for MainForm activates another form.

TMainForm.OnActivate;
begin
ChildForm.ShowModal;
end;

Control focus is set but does not work. The work around I found was sending PostMessage(Handle, WM_SETFOCUS, 0, 0); to the form handle.

procedure TChildForm.FocusControl(AWinControl: TWinControl);
begin
  try
    // http://stackoverflow.com/questions/7305296/tedit-focus-caret
    PostMessage(Handle, WM_SETFOCUS, 0, 0);
    PostMessage(AWinControl.Handle, WM_SETFOCUS, 0, 0); 
    if AWinControl.CanFocus then
       AWinControl.SetFocus;
  except
    on E: Exception do
    begin
      Error(Self, E);
    end;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文