Wel*_*.59 2 c# .net-4.0 winforms
我有一个.NET 4 WinForm应用程序.我在主窗体上有一个打开子窗体的按钮.儿童表格有焦点.当子窗体打开时,如果我单击主窗体,主窗体将获得焦点,但子窗体仍保留在主窗体的顶部.
即使儿童表格打开,我怎样才能使主表格出现在前面?
这是我的两个表单的示例代码:
using System;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var form = new Form2();
form.Show(this);
}
}
}
using System.Windows.Forms;
namespace WinTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据Kumar在下面的回答,我更新了我的主要表格如下:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_FormList = new List<Form>();
}
private List<Form> _FormList;
private void button1_Click(object sender, EventArgs e)
{
var form = new Form2();
form.FormClosed += Form_FormClosed;
_FormList.Add(form);
form.Show();
}
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
_FormList.Remove((Form)sender);
}
private void Form1_Resize(object sender, EventArgs e)
{
foreach (var form in _FormList)
{
form.Visible = WindowState != FormWindowState.Minimized;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这现在按预期工作.
做这个
form.Show();
Run Code Online (Sandbox Code Playgroud)
代替
form.Show(this);
Run Code Online (Sandbox Code Playgroud)
参数设置要显示的表单的父级.父表格将以其子表格形式提供.