R
RizwanSharp
I have a Pocket PC Chat application where i need to open so many forms for private chat sessions. The problem I faced was that when I opened a few form I saw and instance in Task Manager. For 10 opened forms I saw 10 Instances. To work around on it I came to the solution that I have to have Text only on 1 Window of application at a time. So I wrote 2 classes:
1) A FormBase class from which all my forms are inherited:
public partial class BaseFormCommSuiteMobile : Form
{
private string uniqueID;
public BaseFormCommSuiteMobile()
{
InitializeComponent();
this.uniqueID = Guid.NewGuid().ToString();
}
public string UniqueID
{
get { return this.uniqueID; }
}
private void BaseFormCommSuiteMobile_Load(object sender, EventArgs e)
{
OpenedFormsCollection.AddForm(this);
}
private void BaseFormCommSuiteMobile_Closing(object sender, CancelEventArgs e)
{
OpenedFormsCollection.CloseAll();
}
private void BaseFormCommSuiteMobile_Activated(object sender, EventArgs e)
{
this.Text = "XXXX Application";
OpenedFormsCollection.UpdateFormsText(this.uniqueID);
}
}
The Helper class which Removes the Text from all Other Forms except the Active One!
public static class OpenedFormsCollection
{
private static Dictionary<string, BaseFormCommSuiteMobile> openedFormsDictionary = new Dictionary<string, BaseFormCommSuiteMobile>(10);
public static void UpdateFormsText(string uniqueID)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection openedForms = openedFormsDictionary.Values;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
if(form.UniqueID != uniqueID)
{
form.Text = "";
}
}
}
public static void AddForm(BaseFormCommSuiteMobile formToBeAdded)
{
if(!openedFormsDictionary.ContainsKey(formToBeAdded.UniqueID))
{
openedFormsDictionary.Add(formToBeAdded.UniqueID,formToBeAdded);
}
}
public static void RemoveForm(string uniqueID)
{
openedFormsDictionary.Remove(uniqueID);
}
public static void CloseAll()
{
lock (openedFormsDictionary)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection openedForms = openedFormsDictionary.Values;
BaseFormCommSuiteMobile[] forms = new BaseFormCommSuiteMobile[openedForms.Count];
int i = 0;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
forms[i++] = form;
}
for (int loop = i - 1; loop >= 0; loop--)
{
forms[loop].Close();
}
}
}
}
Right! Now I have only one problem. How can I close all Forms when I close the Application from Task Manager. The CloseAll() function in the above class solves this problem but I have to set MinimizeBox property of all the Forms to true get it working. Otherwise If I close a form even not from Task Manager It'll close all other Forms.
I want to set MinimizeBox property of the main Form to true all other forms shuold have this proprty set to false. I want to know is there any way to to solve this problem or any way to detect that if this Form was closed from the Task Manager or from the Close Button.
Any help will be highly appreciated.
Best Regards,
___
Newsgroups brought to you courtesy of www.dotnetjohn.com
1) A FormBase class from which all my forms are inherited:
public partial class BaseFormCommSuiteMobile : Form
{
private string uniqueID;
public BaseFormCommSuiteMobile()
{
InitializeComponent();
this.uniqueID = Guid.NewGuid().ToString();
}
public string UniqueID
{
get { return this.uniqueID; }
}
private void BaseFormCommSuiteMobile_Load(object sender, EventArgs e)
{
OpenedFormsCollection.AddForm(this);
}
private void BaseFormCommSuiteMobile_Closing(object sender, CancelEventArgs e)
{
OpenedFormsCollection.CloseAll();
}
private void BaseFormCommSuiteMobile_Activated(object sender, EventArgs e)
{
this.Text = "XXXX Application";
OpenedFormsCollection.UpdateFormsText(this.uniqueID);
}
}
The Helper class which Removes the Text from all Other Forms except the Active One!
public static class OpenedFormsCollection
{
private static Dictionary<string, BaseFormCommSuiteMobile> openedFormsDictionary = new Dictionary<string, BaseFormCommSuiteMobile>(10);
public static void UpdateFormsText(string uniqueID)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection openedForms = openedFormsDictionary.Values;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
if(form.UniqueID != uniqueID)
{
form.Text = "";
}
}
}
public static void AddForm(BaseFormCommSuiteMobile formToBeAdded)
{
if(!openedFormsDictionary.ContainsKey(formToBeAdded.UniqueID))
{
openedFormsDictionary.Add(formToBeAdded.UniqueID,formToBeAdded);
}
}
public static void RemoveForm(string uniqueID)
{
openedFormsDictionary.Remove(uniqueID);
}
public static void CloseAll()
{
lock (openedFormsDictionary)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection openedForms = openedFormsDictionary.Values;
BaseFormCommSuiteMobile[] forms = new BaseFormCommSuiteMobile[openedForms.Count];
int i = 0;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
forms[i++] = form;
}
for (int loop = i - 1; loop >= 0; loop--)
{
forms[loop].Close();
}
}
}
}
Right! Now I have only one problem. How can I close all Forms when I close the Application from Task Manager. The CloseAll() function in the above class solves this problem but I have to set MinimizeBox property of all the Forms to true get it working. Otherwise If I close a form even not from Task Manager It'll close all other Forms.
I want to set MinimizeBox property of the main Form to true all other forms shuold have this proprty set to false. I want to know is there any way to to solve this problem or any way to detect that if this Form was closed from the Task Manager or from the Close Button.
Any help will be highly appreciated.
Best Regards,
___
Newsgroups brought to you courtesy of www.dotnetjohn.com