Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to check form already show?
Message
From
11/11/2011 00:30:42
 
 
To
10/11/2011 20:41:30
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows XP
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01528544
Message ID:
01528643
Views:
35
Hi Alex,

I've used a FormsHandler class for that, with static methods. I think static methods are called shared in VB. Here's some C# code for such a class (you can convert it to VB with one of the online converters):

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();
}
This could probably be changed to use a generic List< Form > instead of an ArrayList, but it's existing code I had written quite awhile ago and I've never gotten around to updating it. It works fine the way it is.

~~Bonnie



>because i have many button will call some of form, but if i click the button once time will show the form.
>And click the button second time also show the form , will show the 2 same form.
>
>So i want to check the form already show when user again click this button , then will Activate.
>But how to check the form already use to show.
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