Find the open forms in c# windows application

前端 未结 4 1680
时光说笑
时光说笑 2020-12-17 16:55

I am using this function to close existing form and open a new form.

If there is no exixting form, it throws error.

Error :

Target : System.Object

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 17:49

    if (ApplicationFormStatus.CheckIfFormIsOpen("FormName")) 
    {
    // It means it exists, so close the form
    }
    
     public bool CheckIfFormIsOpen(string formname)
            {
    
                //FormCollection fc = Application.OpenForms;
                //foreach (Form frm in fc)
                //{
                //    if (frm.Name == formname)
                //    {
                //        return true;
                //    }
                //}
                //return false;
    
                bool formOpen= Application.OpenForms.Cast
    ().Any(form => form.Name == formname); return formOpen; }

    I have pasted the two methods one simple one and the second one is the LINQ.

提交回复
热议问题