C#形式的Activated和Deactivate事件 [英] C# form Activated and Deactivate events

查看:498
本文介绍了C#形式的Activated和Deactivate事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,mainForm和subForm.当mainForm失去焦点时,我希望子窗体消失,然后在mainForm重新获得焦点时重新出现.我在mainForm上使用Activated和Deactivate事件来跟踪mainForm是否具有焦点.当激活被激活时,我执行subForm.Show(),而对Deactivate执行相反的操作.我的问题是,当subForm获得焦点时mainForm消失了,因为我不知道如何以编程方式说当mainForm的Deactivate事件触发时,使subForm消失,除非是因为subForm获得了焦点.是当主窗体由于用户单击另一个应用程序或使用ALT + TAB进行切换而失去焦点时,使两个窗口都消失.我不想把子窗体抛在后面.有什么方法可以检查,因为Deactive会触发它是否是因为属于该应用程序的另一种形式获得了关注,而不是其他某种应用程序?

I have two forms, mainForm and subForm. When mainForm loses focus I want subForm to disappear and then reappear as mainForm regains focus. I'm using the Activated and Deactivate events on the mainForm to keep track of whether mainForm has focus or not. When the Activated is fired I do subForm.Show() and the opposite for Deactivate. The problem I have is that when subForm gains focus mainForm disappear because I don't know how to say programmatically "make subForm disappear when the mainForm's Deactivate event fires except if it's because the subForm gained focus. The whole point of what I'm doing is to make both windows disappear when the mainForm loses focus because the user clicked on another application or use ALT+TAB to switch. I don't want to leave the subForm behind. Is there any way of checking as the Deactive fires whether it was because another form belonging to the application gained focus as opposed to some other application?

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.
    }
}

推荐答案

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆