我有两种形式,mainForm 和 subForm。当 mainForm 失去焦点时,我希望 subForm 消失,然后在 mainForm 重新获得焦点时重新出现。我使用 mainForm 上的 Activated 和 Deactivate 事件来跟踪 mainForm 是否有焦点。当 Activated 被触发时,我执行 subForm.Show() 和相反的 Deactivate。我遇到的问题是,当 subForm 获得焦点时 mainForm 消失,因为我不知道如何以编程方式说“当 mainForm 的 Deactivate 事件触发时使 subForm 消失,除非是因为 subForm 获得了焦点。我在做什么就是在mainForm因为用户点击另一个应用或者使用ALT+TAB切换而失去焦点时让两个窗口都消失。我不想把subForm丢在后面。
class MainForm : Form
{
SubForm subForm = new SubForm();
private void mainForm_Activated(object sender, EventArgs e)
{
this.subForm.Show();
}
private void mainForm_Deactivate(object sender, EventArgs e)
{
this.subForm.Hide()
// I need some logic to make sure that it is only hidden
// when the mainForm loses focus because the user clicked
// some other application in the taskbar and not when the
// subForm itself gains the focus.
}
}