为什么表单无法获得焦点? [英] Why Won't Form Get Focus?
问题描述
我有一个像这样模态启动的表单:
I have a form that is launched modally like this:
private void find_street_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
Form findForm = new FindStreet();
findForm.ShowDialog();
this.WindowState = FormWindowState.Normal;
}
表单正确启动,光标位于第一个文本框中,其 TabIndex 设置为 1.
The form launches correctly, and the cursor is in the first text box, whose TabIndex is set to 1.
与 InitializeComponent();
调用一起,这些命令也存在.
Along with the InitializeComponent();
call, these commands are present.
public FindStreet()
{
InitializeComponent();
this.TopMost = true;
this.BringToFront();
this.Focus();
}
我查看并尝试了许多示例.光标出现在正确的控件中,但窗体的窗口没有焦点.问题是,如果用户开始输入,即使新启动的表单可见,这些击键也不会进入文本框.
I have looked at and tried a number of examples. The cursor appears in the correct control, but the form's window does not have the focus. The problem is that if a user starts typing, even though the newly launched form is visible, those keystrokes are not going into the text box.
推荐答案
删除public FindStreet()中的代码,并在FindStreet的加载事件中添加:
Remove the code in public FindStreet() and in load event of FindStreet add:
this.TopMost = true; //i don't know why you need this.
this.Activate();
当您最小化主窗体时,z 顺序中的下一个获取光标.this.Focus() 什么都不做.您需要激活对话框.
When you minimize your main form the next one in z-order get the cursor. this.Focus() doesn't do anything. You need to Activate the dialog.
这篇关于为什么表单无法获得焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!