C# Windows 窗体:最上面的对话框未聚焦
我的应用程序在启动时显示一个登录框,我已经能够将其置于最顶层,但是直到我单击它才将其设置为焦点。
你如何让它自动聚焦?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以显式调用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.)