如何获得不可见的表格? [英] How to get form when it is which is not visible?

查看:70
本文介绍了如何获得不可见的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取打开但隐藏的表格.我已经尝试过了.我得到了表单,但在这种情况下,表单在不到一秒的时间内显示并隐藏.如果我跳过 mfrm.Show(),则不会在 Application.OpenForms 中获得MailSynchronize表单.

I want to get form which is open but hidden. I have tried by this. I get the form but in this case form show and hide within fraction of second. If I skip mfrm.Show(), I don't get MailSynchronize form in Application.OpenForms.

MailSynchronize mfrm = new MailSynchronize();
mfrm.Show();
mfrm.Hide();

我通过以下方法获得表格.

I get form by following method.

foreach (Form f in Application.OpenForms)    //it will return all the open forms
{
     if (f.Name == "MailSynchronize")
     {
          mfrm = (MailSynchronize)f;
          break;
     }
}

有人可以建议我如何获取默认情况下隐藏的打开表单,我可以进入 Application.OpenForms 吗?

Can anybody please suggest me how to get open form which is hidden by default and I can get in Application.OpenForms?

推荐答案

如果我隐藏表单,它是否存在于Application.OpenForms中?

否,不幸的是,如果您隐藏表单,则它不会出现在 Application.OpenForms

No, unfortunately if you Hide a form, it will not be present in Application.OpenForms

那么如何打开不可见的表单?我也希望它存在于Application.OpenForms中.

如果您想打开一个不可见的窗体,并且希望它存在于Application.OpenForms中,则可以使用此代码,而不仅仅是 Show():

If you want to open an invisible Form, and you want it want it to exists in Application.OpenForms, you can use this code instead of simply Show():

var f = new MailSynchronize();
f.Opacity = 0; 
f.ShowInTaskbar = false;
f.Show();

如何再次查找该表格?

要获取表单的打开实例,可以使用 Application.OfType< MailSynchronize>()

To get the open instance of form you can use Application.OfType<MailSynchronize>()

var f= Application.OpenForms.OfType<MailSynchronize>()
                  .FirstOrDefault();

找到后,如何再次显示?

f.Opacity = 1;
f.ShowInTaskbar = true;
f.Show();

如何再次隐藏它?

您不应调用 Hide()来隐藏表单,因为它会使表单脱离 Application.OpenForms ,而应使用以下方式:

You should not call Hide() to hide the form because it makes the form to get out of Application.OpenForms, instead you should use this way:

f.Opacity = 0; 
f.ShowInTaskbar = false;

还有其他方法吗?

是的,例如,您可以通过以下方式在类中(例如,在 Program.cs 中)创建静态属性:

Yes, for example you can create an static property in a class, for example in Program.cs this way:

public static MailSynchronize MailSynchronizeInstance { get; set; }

,并且是第一次打开表单时,可以将实例分配给此属性,然后可以使用Program.MailSynchronizeInstance来显示或隐藏它,而无需查看 Application.OpenForms 或执行解决方法.

and the first time you want to open your form, you can assign the instance to this property, and then you can use it using Program.MailSynchronizeInstance to show or hide and you don't need to look in Application.OpenForms or perform a workaround.

您还可以单调方式设置此属性.

Also you can make this property in a singletone way.

这篇关于如何获得不可见的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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