表单在 C# 中失去焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/570021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:37:42  来源:igfitidea点击:

Form's lost focus in C#

c#winforms.net-2.0focus

提问by

This may be a simple C# question but I need a solution.

这可能是一个简单的 C# 问题,但我需要一个解决方案。

I have two forms, form1and form2, with form1having a button. On the clickof the button, I want to show form2. When form2looses focus I want to hide it (form2). How can I do this? I tried to subscribe to the LostFocusevent of form2, but it isn't working.

我有两个表单,form1form2form1有一个button。在点击按钮的,我想展示窗口2。当form2失去焦点时,我想隐藏它(form2)。我怎样才能做到这一点?我试图订阅form2LostFocus事件,但它不起作用。

Please help me with this.

请帮我解决一下这个。

Note -- I use .Net 2.0

注意——我使用 .Net 2.0

采纳答案by Jcl

Use the Deactivateevent handler

使用Deactivate事件处理程序

回答by Kent Boogaart

If I understand your question, I think you actually want to trap deactivation. Button handler inside your main form:

如果我理解您的问题,我认为您实际上想要陷阱停用。主窗体中的按钮处理程序:

private void button1_Click(object sender, EventArgs e)
{
    Form childForm = new Form();
    childForm.Deactivate += delegate
    {
        childForm.Close();
    };

    childForm.Show();
}