How to get form when it is which is not visible?

╄→гoц情女王★ 提交于 2019-12-25 16:57:12

问题


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

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


回答1:


If I Hide a form, does it exist in Application.OpenForms?

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

So how can I open an invisible Form? Also I want it to exists in Application.OpenForms.

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();

How to find that form again?

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

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

When I found it, How to show it again?

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

How to hide it again?

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;

Is there another way?

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

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.




回答2:


EDIT

This should work for your specific case now:

this.Opacity = 0;
this.ShowInTaskbar = false;

When you add these 2 codelines in your MailSynchronize constructor the form will start minimized but will not show in your taskbar, which is essentially the effect you were looking for. Also the form will now popup in your Application.OpenForms Collection.




回答3:


use f.Visible (return type is bool)

if it returns false, it means form is hidden. If it returns true then form is visible.




回答4:


When form initiallize.

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

How to find that form again?

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

When I found it, How to show it again?

mfrm.Opacity = 1;
mfrm.Show();

Hide again by Button.

mfrm.Hide();    //It will not show form in Application.OpenForms if I hide again by mfrm.Opacity = 0;


来源:https://stackoverflow.com/questions/33100860/how-to-get-form-when-it-is-which-is-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!