uncle hammy

Hey All,

I have seen numerous posts where people mention building a form manager to keep track of open forms and other settings.

Can anyone point me to an example of such an animal

I want to build a manager that when a new form is opened, it is stored in the central location. I also want to be able to go to a central location to check the "Current User" as this is an application class, and is easily and frequently switched on the fly by the user. Basically a central location to go to that is up to date with the current environment.

However, I am not quite sure where to start, and an example of one would be nice.

Thanks,

Hammy



Re: Windows Forms General Building a Form Manager

DMan1

How about VB.NET 05 and the MY namespace....a great quick proxy for user, computer, filessystem, and application properties including OpenForms

Now just build a good winform GUI to retrieve and display the data






Re: Windows Forms General Building a Form Manager

nobugz

The System.Environment and System.Windows.Forms.Application classes contain just about all you need here. Environment.UserName contains the logged-on user name. Application.OpenForms gives you all the forms that are created.





Re: Windows Forms General Building a Form Manager

uncle hammy

OK.

I have already been looping through open forms to find things I am looking for. I wanted to see examples of the way others were doing it, to see if there was a more effecient way of doing it.

I am not interested in the logged on user, I am interested in the "Application Selected User", as I said it is an application specific class.





Re: Windows Forms General Building a Form Manager

nobugz

Rather than trying to find forms back that have the info you need, consider a dedicated class to stores application state. Since this info has application scope, you can use a static class, making it trivial to access it the info from other forms. If you tell me the language you'll use, I show you an example.





Re: Windows Forms General Building a Form Manager

uncle hammy

I am using C#. I still not have come up with a good way of doing this. I am currently looping through the Application.OpenForms to look for specific forms (or groups of the same form), however this seems time consuming to me...

public static List<Form> GetOpenFormsByType(string FormType)

{

// list of forms to return
List<Form> formList = new List<Form>();

// get the type of the form being looked for
Type type = Type.GetType(FormType);

// loop through all forms and if the form type is in the list add it to our return list
foreach (Form thisForm in Application.OpenForms)
{
if (thisForm.GetType() == type)
formList.Add(thisForm);
}

return formList;

}





Re: Windows Forms General Building a Form Manager

BonnieB

Here's the way I do it:

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();

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();
}






Re: Windows Forms General Building a Form Manager

uncle hammy

Thanks for the start!

I have 2 questions....

1. I understand how the form handler works. What I don't understand is how you create a single instance of it that become easily accessible from anywhere in the application.

2. What do you do if you want to get all the "abc" forms from the handler, or the main form from the handler





Re: Windows Forms General Building a Form Manager

BonnieB

That's the point with static methods ... you don't need an instance of the FormsHandler class, you just access it's static methods anywhere.

As far as your second question, you'd need to add more static methods to your FormsHandler class to do searches in the collection.