Form.Hide() and Form.Show()

J

johnathan.miles

Hi all,

I'm trying to optimise an existing mobile application, written in C#.
One thing I would like to do is have each of the forms in the
application loaded only once, because the initialisecomponent() call
takes too long.

Originally the code read something like this:

<in the MainMenu form>
Form1 otherform = new Form1(this); // passing in MainMenu as a
parameter called ParentForm

otherform.Show();

<and then in a 'CloseForm' function on Form1 it does something like:>

ParentForm.Show();
this.Dispose();


Instead of doing that, which meant that the next time a user wishes to
see Form1 the app has to call initializecomponent for it again, I have
created a singleton 'FormManager' which contains an instance of Form1.
So now to display I just get the instance of my FormManager and ask for
it's copy of Form1, on which I then call 'show()'.

This works the first time the form is displayed. In the CloseForm
function on Form1 I then call 'this.hide()' and 'ParentForm.show()',
which gets me back nicely to the main menu. The problem is though, when
I get my instance of Form1 from the FormManager a second time and call
'Show' nothing happens.

Anyone got any idea what I'm doing wrong?? I've very much a CF newbie-
having just inherited this product....
 
J

johnathan.miles

Looking at it again, the 'Dispose' method of Form1 is being called just
after parentform.Show() when using my singleton, even though the
singleton still has a reference to it, and I'm no longer explicitly
calling Dispose(). Does anyone know why it would do this?
 
J

johnathan.miles

Solved my own problem- I needed to set the CancelEvent argument in my
Form_Closing event to Cancel=true to stop it disposing of itself :)
 

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