Hi all,
I'm new to the .NET compact framework (and pretty much new to C# as well) and I've got a problem with switching forms in my mobile application. At a high level, my application makes use of multiple forms, with a main "application manager" class which performs the navigation/switching between the forms. My plan was to create forms on demand, cache them and use a simple hide/show strategy.
At first I wanted to do something like the following in my main application class:
public void switchForm(Form newForm)
{
currentForm.Hide(); // instance member
newForm.Show();
currentForm = newForm;
}
However, this did not work out as planned. The new form that I was attempting to show, appeared very temporarily and then disappeared behind the main form of my application - any ideas as to why this happens? I've done quite a bit of reading on form switching, and most places seemed to mention that the above method was the way to do it.
I then tried out the following:
public void switchForm(Form newForm)
{
currentForm.Hide(); // instance member
currentForm = newForm;
newForm.ShowDialog();
}
and this seems to do the trick. I'm wondering, however, is this the right way to go about it? Using the ShowDialog() method, however, poses another problem. Say I want to dispose of the old form, like so:
public void switchForm(Form newForm)
{
Form oldForm = currentForm;
currentForm = newForm;
newForm.ShowDialog();
oldForm.Dispose();
}
I then found out that the oldForm.Dispose() code does not execute until the newForm.ShowDialog() completes (the form is closed). Therefore, the above can lead to leaks with the forms not being disposed correctly as the method is called over and over again while moving to new forms. Another way is to dispose the old form first before showing the new one, however, then my application temporarily renders something else (whatever was behind the forms) between the old form being disposed and the new one being rendered :/ How should one go about doing something like this?
Thanks very much.