How to know if a "Form" is currently running in background ?

P

ppcdev

Hi.

Actually, i'm working on C# projects in some french companies.
Would anyone know how to know if a "form" is currently running in
background ?
Actually, I've implemented the following test in an application :


Form myFormHandle = new Form1();
this.mySavedForm1 = myFormHandle;
Form myFormHandle2 = new Form2();
this.mySavedForm2 = myFormHandle2;
public Form mySavedForm1;
public Form mySavedForm3;
....doing many many processes...
....then switch to form2 (eg: Form2.Show());


When I need to go back to Form1 from Form2, here's how I think :

"Ok, starting point = I'm on Form2"
"Is Form1 already existing ?"
"If yes, just show it"
"If not, recreate it"

Here's how I translate this into the computer world :
(I'm on Form2)

if (Form1.mySavedForm1!=null) Form1.mySavedForm1.Show()
else
Form1 form1 = new Form1();
form1.mySaveForm1 = form1;
form1.mySavedForm1.Show();



..... The problem seems to be on the "null" comparison... Is is a
correct thing to compare form handles to null ?

Thank you very much, microsoftmen!
 
1

100

Hi ppcdev,
Testing for null won't give you correct results. It will allways be not null
because you keep reference to the form all the time.
Whether you have to check the form depends on what you do when you switch to
the second form. If you call close that will dispose the form and you cannot
use Show method you have to instantiate a new one. If you use Hide, though,
you don't have to create the form again you can use Show to show it again.
If you don't use Close and you keep a reference to the form all the time
(and obviously you do) you don't have to worry about the form it won't
dispose itself.

However to check whether the form is ok to be shown you can check Disposing
and IsDisposed form's proerties. If one of them is true you have to
instantiate a new form.

HTH
B\rgds
100
 
P

ppcdev

Thank you very much for your explanations, Mister Hundred ;)
Have to experience C# a little more since now :))))
See ya
 

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