Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Winforms handler
Message
From
08/01/2007 09:39:26
 
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01183380
Message ID:
01183430
Views:
18
This message has been marked as the solution to the initial question of the thread.
First the FormsHandler class. Notice the static methods.
using System;
using System.Collections;
using System.Windows.Forms;

namespace MyNameSpace.MyClasses
{
	public class FormsHandler
	{
		#region Declarations
		private static ArrayList list = new ArrayList();
		#endregion

		#region Methods
		public static int Add(object o)
		{
			return list.Add(o);
		}
		public static void Remove(object o)
		{
			list.Remove(o);
		}
		public static bool Close()
		{
			int nCount = list.Count;
			while (list.Count > 0)
			{
				((Form)list[0]).Close();
				if (list.Count == nCount)
					return false;
				else
					nCount = list.Count;
			}

			return true;
		}
		#endregion

		#region Properties
		public static ArrayList List
		{
			get {return list;}
		}
		#endregion
	}
}
Whenever you open a form, no matter where you open it from, all you do is add it to the ArrayList, like this:
Form oForm = new MyForm();
FormsHandler.Add(oForm);
oForm.Show();
When you close your Main Form, you want all other's to Close (but to execute their own Closing methods) ... do it like this:
// This is a menu item that exits the application

private void menuItem4_Click(object sender, System.EventArgs e)
{
	System.ComponentModel.CancelEventArgs ee = new CancelEventArgs();
	this.ClosingHandler(sender, ee);
}

// This is the ClosingHandler that will execute normally if you close the app
// by clicking on the "X"

private void ClosingHandler(object sender, System.ComponentModel.CancelEventArgs e)
{
	if (!FormsHandler.Close())
		e.Cancel = true;
	else
		Application.Exit();
}
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform