So many Forms Issue Still not solved :(

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
 
F

Fabien

Hi,

In the BaseFormCommSuiteMobile_Closing method, check if you are the
main Window and if it is the case, call
OpenedFormsCollection.CloseAll(), else just close you current
window....

Hope this help you.

BR


Fabien Decret
Windows Embedded Consultant
C# MCP


ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/




RizwanSharp a écrit :
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
 
R

RizwanSharp

Hi Fabien,
Thank You so much for reading a large question, explaination and looking insight in the code.
Right! That's done, You gave me a little push towards solution! There is still one problem. What if a Child form is shown in Task Manager? A user will assume that he can close application from the Task Manager by closing that Form from there but in Closing when I check if this is not main then do not Close All forms :( Here is the problem. I want to know is there any way to detect that if that Closing event was seed of Task Manager Close operation or direct by the user. May be some Windows Message Can help in this. So I see if it was closed from the task manager then close all forms no matter what it was a main Form or other forms other wise close only current form.
I hope you understand what I mean!
In last, I thank you again for looking insight in my problem and giving a nice situation which leads toward success.
Best Regards,
Rizwan

In the BaseFormCommSuiteMobile_Closing method, check if you are the
main Window and if it is the case, call
OpenedFormsCollection.CloseAll(), else just close you current
window....

Hope this help you.

BR


Fabien Decret
Windows Embedded Consultant
C# MCP


ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/




RizwanSharp a =E9crit :
I have a Pocket PC Chat application where i need to open so many forms fo=
r private chat sessions. The problem I faced was that when I opened a few f=
orm I saw and instance in Task Manager. For 10 opened forms I saw 10 Instan=
ces. To work around on it I came to the solution that I have to have Text o=
nly 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 =3D Guid.NewGuid().ToString();
}

public string UniqueID
{
get { return this.uniqueID; }
}

private void BaseFormCommSuiteMobile_Load(object sender, EventArg= s e)
{
OpenedFormsCollection.AddForm(this);
}

private void BaseFormCommSuiteMobile_Closing(object sender, Cance= lEventArgs e)
{
OpenedFormsCollection.CloseAll();
}

private void BaseFormCommSuiteMobile_Activated(object sender, Eve= ntArgs e)
{
this.Text =3D "XXXX Application";
OpenedFormsCollection.UpdateFormsText(this.uniqueID);
}
}

The Helper class which Removes the Text from all Other Forms except the A= ctive One!

public static class OpenedFormsCollection
{
private static Dictionary<string, BaseFormCommSuiteMobile> opened=
FormsDictionary =3D new Dictionary said:
public static void UpdateFormsText(string uniqueID)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection o=
penedForms =3D openedFormsDictionary.Values;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
if(form.UniqueID !=3D uniqueID)
{
form.Text =3D "";
}
}
}

public static void AddForm(BaseFormCommSuiteMobile formToBeAdded)
{
if(!openedFormsDictionary.ContainsKey(formToBeAdded.UniqueID))
{
openedFormsDictionary.Add(formToBeAdded.UniqueID,formToBe= Added);
}
}

public static void RemoveForm(string uniqueID)
{
openedFormsDictionary.Remove(uniqueID);
}

public static void CloseAll()
{
lock (openedFormsDictionary)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollecti=
on openedForms =3D openedFormsDictionary.Values;
BaseFormCommSuiteMobile[] forms =3D new BaseFormCommSuite= Mobile[openedForms.Count];
int i =3D 0;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
forms[i++] =3D form;
}

for (int loop =3D i - 1; loop >=3D 0; loop--)
{
forms[loop].Close();
}
}
}
}

Right! Now I have only one problem. How can I close all Forms when I clos=
e the Application from Task Manager. The CloseAll() function in the above c=
lass 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 Tas=
k Manager It'll close all other Forms.
I want to set MinimizeBox property of the main Form to true all other for=
ms shuold have this proprty set to false. I want to know is there any way t=
o to solve this problem or any way to detect that if this Form was closed f=
rom the Task Manager or from the Close Button.
Any help will be highly appreciated.

Best Regards,
=20
=20
___
Newsgroups brought to you courtesy of www.dotnetjohn.com



___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
R

RizwanSharp

Hi Fabien,
Thank You so much for reading a large question, explaination and looking insight in the code.
Right! That's done, You gave me a little push towards solution! There is still one problem. What if a Child form is shown in Task Manager? A user will assume that he can close application from the Task Manager by closing that Form from there but in Closing when I check if this is not main then do not Close All forms :( Here is the problem. I want to know is there any way to detect that if that Closing event was seed of Task Manager Close operation or direct by the user. May be some Windows Message Can help in this. So I see if it was closed from the task manager then close all forms no matter what it was a main Form or other forms other wise close only current form.
I hope you understand what I mean!
In last, I thank you again for looking insight in my problem and giving a nice situation which leads toward success.
Best Regards,
Rizwan

In the BaseFormCommSuiteMobile_Closing method, check if you are the
main Window and if it is the case, call
OpenedFormsCollection.CloseAll(), else just close you current
window....

Hope this help you.

BR


Fabien Decret
Windows Embedded Consultant
C# MCP


ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/




RizwanSharp a =E9crit :
I have a Pocket PC Chat application where i need to open so many forms fo=
r private chat sessions. The problem I faced was that when I opened a few f=
orm I saw and instance in Task Manager. For 10 opened forms I saw 10 Instan=
ces. To work around on it I came to the solution that I have to have Text o=
nly 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 =3D Guid.NewGuid().ToString();
}

public string UniqueID
{
get { return this.uniqueID; }
}

private void BaseFormCommSuiteMobile_Load(object sender, EventArg= s e)
{
OpenedFormsCollection.AddForm(this);
}

private void BaseFormCommSuiteMobile_Closing(object sender, Cance= lEventArgs e)
{
OpenedFormsCollection.CloseAll();
}

private void BaseFormCommSuiteMobile_Activated(object sender, Eve= ntArgs e)
{
this.Text =3D "XXXX Application";
OpenedFormsCollection.UpdateFormsText(this.uniqueID);
}
}

The Helper class which Removes the Text from all Other Forms except the A= ctive One!

public static class OpenedFormsCollection
{
private static Dictionary<string, BaseFormCommSuiteMobile> opened=
FormsDictionary =3D new Dictionary said:
public static void UpdateFormsText(string uniqueID)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollection o=
penedForms =3D openedFormsDictionary.Values;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
if(form.UniqueID !=3D uniqueID)
{
form.Text =3D "";
}
}
}

public static void AddForm(BaseFormCommSuiteMobile formToBeAdded)
{
if(!openedFormsDictionary.ContainsKey(formToBeAdded.UniqueID))
{
openedFormsDictionary.Add(formToBeAdded.UniqueID,formToBe= Added);
}
}

public static void RemoveForm(string uniqueID)
{
openedFormsDictionary.Remove(uniqueID);
}

public static void CloseAll()
{
lock (openedFormsDictionary)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollecti=
on openedForms =3D openedFormsDictionary.Values;
BaseFormCommSuiteMobile[] forms =3D new BaseFormCommSuite= Mobile[openedForms.Count];
int i =3D 0;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
forms[i++] =3D form;
}

for (int loop =3D i - 1; loop >=3D 0; loop--)
{
forms[loop].Close();
}
}
}
}

Right! Now I have only one problem. How can I close all Forms when I clos=
e the Application from Task Manager. The CloseAll() function in the above c=
lass 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 Tas=
k Manager It'll close all other Forms.
I want to set MinimizeBox property of the main Form to true all other for=
ms shuold have this proprty set to false. I want to know is there any way t=
o to solve this problem or any way to detect that if this Form was closed f=
rom the Task Manager or from the Close Button.
Any help will be highly appreciated.

Best Regards,
=20
=20
___
Newsgroups brought to you courtesy of www.dotnetjohn.com



___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 
F

Fabien

Hi Rizwan,

Your problem is not very simple, and I havn't any solution, but I think
Window Message is a good idea, check out this link, you can catch the
WM_DESTROY message in the WndProc :
http://blogs.msdn.com/netcfteam/archive/2005/07/24/442616.aspx
Then, I don't know how to know who has sent the message...

Hope this help you.

BR

Fabien Decret
Windows Embedded Consultant
C# MCP

RizwanSharp a écrit :
Hi Fabien,
Thank You so much for reading a large question, explaination and looking insight in the code.
Right! That's done, You gave me a little push towards solution! There is still one problem. What if a Child form is shown in Task Manager? A user will assume that he can close application from the Task Manager by closing that Form from there but in Closing when I check if this is not main then do not Close All forms :( Here is the problem. I want to know is there any wayto detect that if that Closing event was seed of Task Manager Close operation or direct by the user. May be some Windows Message Can help in this. SoI see if it was closed from the task manager then close all forms no matter what it was a main Form or other forms other wise close only current form.
I hope you understand what I mean!
In last, I thank you again for looking insight in my problem and giving anice situation which leads toward success.
Best Regards,
Rizwan

In the BaseFormCommSuiteMobile_Closing method, check if you are the
main Window and if it is the case, call
OpenedFormsCollection.CloseAll(), else just close you current
window....

Hope this help you.

BR


Fabien Decret
Windows Embedded Consultant
C# MCP


ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/




RizwanSharp a =E9crit :
I have a Pocket PC Chat application where i need to open so many forms fo=
r private chat sessions. The problem I faced was that when I opened a fewf=
orm I saw and instance in Task Manager. For 10 opened forms I saw 10 Instan=
ces. To work around on it I came to the solution that I have to have Texto=
nly 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 =3D Guid.NewGuid().ToString();
}

public string UniqueID
{
get { return this.uniqueID; }
}

private void BaseFormCommSuiteMobile_Load(object sender, EventArg= s e)
{
OpenedFormsCollection.AddForm(this);
}

private void BaseFormCommSuiteMobile_Closing(object sender, Cance= lEventArgs e)
{
OpenedFormsCollection.CloseAll();
}

private void BaseFormCommSuiteMobile_Activated(object sender, Eve= ntArgs e)
{
this.Text =3D "XXXX Application";
OpenedFormsCollection.UpdateFormsText(this.uniqueID);
}
}

The Helper class which Removes the Text from all Other Forms except theA= ctive One!

public static class OpenedFormsCollection
{
private static Dictionary<string, BaseFormCommSuiteMobile> opened=
FormsDictionary =3D new Dictionary said:
public static void UpdateFormsText(string uniqueID)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollectiono=
penedForms =3D openedFormsDictionary.Values;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
if(form.UniqueID !=3D uniqueID)
{
form.Text =3D "";
}
}
}

public static void AddForm(BaseFormCommSuiteMobile formToBeAdded)
{
if(!openedFormsDictionary.ContainsKey(formToBeAdded.UniqueID))
{
openedFormsDictionary.Add(formToBeAdded.UniqueID,formToBe= Added);
}
}

public static void RemoveForm(string uniqueID)
{
openedFormsDictionary.Remove(uniqueID);
}

public static void CloseAll()
{
lock (openedFormsDictionary)
{
Dictionary<string, BaseFormCommSuiteMobile>.ValueCollecti=
on openedForms =3D openedFormsDictionary.Values;
BaseFormCommSuiteMobile[] forms =3D new BaseFormCommSuite= Mobile[openedForms.Count];
int i =3D 0;
foreach (BaseFormCommSuiteMobile form in openedForms)
{
forms[i++] =3D form;
}

for (int loop =3D i - 1; loop >=3D 0; loop--)
{
forms[loop].Close();
}
}
}
}

Right! Now I have only one problem. How can I close all Forms when I clos=
e the Application from Task Manager. The CloseAll() function in the abovec=
lass 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 Tas=
k Manager It'll close all other Forms.
I want to set MinimizeBox property of the main Form to true all other for=
ms shuold have this proprty set to false. I want to know is there any wayt=
o to solve this problem or any way to detect that if this Form was closedf=
rom the Task Manager or from the Close Button.
Any help will be highly appreciated.

Best Regards,
=20
=20
___
Newsgroups brought to you courtesy of www.dotnetjohn.com



___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top