Closing modal dialogs

Class library usage, coding and language questions.
bsing
Posts: 81
Joined: Tue Feb 07, 2012 5:25 am

Closing modal dialogs

Post by bsing » Thu Aug 16, 2012 12:52 am

Hi there,

I have created a general code module that identifies all forms with a particular title, and then attempts to close them. If changes have been made on the form, a popup modal dialog (form) will appear asking to save, not to save and cancel the changes (yes, no and cancel).

The code below shows my solution. It works well ... but was just wondering if you could recommend a more elegant / software engineered way of coding this?

Code: Select all

private void closeOpenForms()
        {        	
        	IList<Form> allForms = Host.Local.FindChildren<Form>();
                        
            foreach (Form frm in allForms)
            {           	           	
            	// close all Property Pages
            	if (frm.Title.Contains("Properties"))
            	{
              		Thread dialogWatcher = new Thread(ClosePopUpDialog);
            		dialogWatcher.IsBackground = true;
            		dialogWatcher.SetApartmentState(ApartmentState.STA);
            		dialogWatcher.Start();
            		
            		frm.Close();            		
            	}
        	}
    	}
                        
        private void ClosePopUpDialog()
        {            	        	
        	while (true)
        	{
        		Thread.Sleep(1000);
        		        		
        		IList<Form> allForms = Host.Local.FindChildren<Form>();
        			
        		foreach (Form fm in allForms)
        		{
        			if (fm.Title.Contains("Properties"))
        			{
        				IList<Button> allButtons = fm.FindChildren<Button>();
        				
        				foreach (Button b in allButtons)
        				{
        					if (b.Text.Equals("&No"))
        					{
        						b.Focus();
        						b.EnsureVisible();	
        						b.Click();
        						return;
        					}
        				}
        			}
        		}
        		
        		return;
        	}        	
        }
Thanks.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Closing modal dialogs

Post by Support Team » Thu Aug 16, 2012 3:08 pm

Hi,

There is a blog about this topic: Handling dialog and pop up windows.
In my honest opinion you should just create one Thread and not a Thread for each Form, this would be an overkill ;).
I would make it as it is described in the blog ;).

Regards,
Markus
Ranorex Support Team
.
Image