C# Windows 窗体:最上面的对话框未聚焦

发布于 2024-08-12 10:40:57 字数 74 浏览 1 评论 0原文

我的应用程序在启动时显示一个登录框,我已经能够将其置于最顶层,但是直到我单击它才将其设置为焦点。

你如何让它自动聚焦?

My app displays a login box on start up, I've been able to make it top most, however it is not set focused until I click on it.

How do you make it so it's automatically focused?

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

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

发布评论

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

评论(1

雨的味道风的声音 2024-08-19 10:40:57

您可以显式调用Activate(),例如在登录对话框的Load 事件处理程序中。

或者,您可以通过调用 ShowDialog() 方法而不是 Show() 以模态方式显示对话框 — 这应该会使其获得焦点。

我怀疑正在发生的事情是,您在拥有的窗口完全激活之前显示对话框,并且主窗体正在窃取焦点。也许您正在尝试在主窗体的构造函数或加载事件处理程序中显示登录对话框?如果是这种情况,您最好修改引导加载程序 (Program.cs),以在显示主窗体之前显示登录对话框。

(正如已经建议的,如果您需要更好的建议,请发布代码。)

You could call Activate() explicitly, for example in the log-in dialog's Load event handler.

Alternatively, you could show the dialog modally, by calling the ShowDialog() method rather than Show() — that should cause it to be focused.

I suspect what is happening though is that you are showing the dialog before the owning window has fully activated, and that the main form is stealing back focus. Perhaps you are trying to show the log-in dialog in the main form's constructor or Load event handler? If that is the case, you may be better off modifying your bootstrap loader (Program.cs) to show the log-in dialog before showing the main form.

(As already suggested, post the code if you want a better advice.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文