Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Form manager
Message
De
03/03/2012 20:18:44
 
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Titre:
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01536209
Message ID:
01537175
Vues:
65
>Hi all, I have often heard of devs using a *Forms Manager* to launch / track their forms - anyone here using one ? if yes - could you post some code ? just thinking about writing one but I don't want to reinvent...

Your question is a week old, but I'll answer now since no one's helped you out yet. (I've been away at the MVP Summit all last week).

I'm surprised that I haven't written a blog post about this topic yet. Maybe I should, but here's the canned answer I already have for this question (I just noticed that in my code snippets below, written a long time ago, I'm using an ArrayList. This is fine and will work ... but if I were to do this today, I'd probably change the ArrayList to a List< Form >):

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 void Add(Form o)
		{
			Form oForm = Find(o);
			if (oForm == null)
			{
				oForm = o;
				list.Add(o);
			}

			oForm.Show();
		}
		public static Form Find(Form o)
		{
			foreach (Form x in list)
			{
				if (o.GetType().Equals(x.GetType()))
					return x;
			}
			return null;
		}
		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);
In a Form's ClosedHandler() method, you'll want this:
protected virtual void ClosedHandler(object sender, EventArgs e)
{
    FormsHandler.Remove(this)
}
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform