早安,
我想检查是否有人可以协助解决我遇到的以下问题。我有一个使用ShowDialog(this)
显示的子窗体,并且正在注册Resize
事件,以便可以与该子窗体一起更改父窗体的状态。问题在于,当父窗体最小化时,子窗体将关闭。我已经确定,父级中的WindowState = FormWindowState.Minimized
调用会导致using
语句通过ShowDialog(IWin32Window)
的阻塞调用,但是我不明白只有当孩子最小化时,父级才能获得焦点) ,阻止呼叫不会通过。我如何避免这种行为?最好不要保留对子窗口的引用并在每次父窗口的WindowState
更改时都对其进行检查。
我的代码:
private void StockTakeHistoryMenu_Click(object sender, EventArgs e)
{
using (StockTakeWindow window = new StockTakeWindow())
{
window.Resize += this.ChildStateChanging;
window.ShowDialog(this);
window.Resize -= this.ChildStateChanging;
}
}
private void ChildStateChanging(object sender, EventArgs e)
{
switch ((sender as Form).WindowState)
{
case FormWindowState.Normal:
this.WindowState = FormWindowState.Normal;
break;
case FormWindowState.Minimized:
this.WindowState = FormWindowState.Minimized;
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Maximized;
break;
}
}
您可以使用此:
private void ChildStateChanging(object sender, EventArgs e)
{
this.Show();
switch ((sender as Form).WindowState)
{
case FormWindowState.Normal:
this.WindowState = FormWindowState.Normal;
break;
case FormWindowState.Minimized:
//this.WindowState = FormWindowState.Minimized;
this.Hide();
break;
case FormWindowState.Maximized:
this.WindowState = FormWindowState.Maximized;
break;
}
}
我在this.Show();
的第一个中呼叫ChildStateChanging
,然后我写了[[this.Hide()
而不是this.WindowState = FormWindowState.Minimized;
。