方法1, 修改使用 show() 方法, 然后 实例化窗体名.Activate(); 即可使子窗体获得焦点.
方法2, 如果非要使用ShowDialog()打开子窗体 ,那么就调用API,使子窗体获得焦点,方法如下:
(2.1)在子窗体中代码中添加如下代码:
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow(); //此处获得本窗体的句柄
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);//设置指定窗体为活动窗体
(2.2)在子窗体Lod事件中调用方法(示例):
private void TestSubForms_Load(object sender, EventArgs e)
{
SetForegroundWindow(this.Handle);
}
以上实测满足项目需求,感谢参考!