Switching forms in the .NET compact framework (Windows Mobile 6)

I am new to the .NET compact framework (and a lot of new for C #), and I had a problem with switching forms in a mobile application. At a high level, my application uses several forms with the main class "application manager", which performs navigation / switching between 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 as planned. The new form I was trying to show appeared very temporarily and then disappeared behind the main form of my application - any ideas as to why this is happening? I read quite a lot when switching form, and most places seemed to mention that the above method was a way to do this.

Then I tried the following:

 public void switchForm(Form newForm) { currentForm.Hide(); // instance member currentForm = newForm; newForm.ShowDialog(); } 

and that seems to do the trick. I wonder, however, is this the right way? However, using the ShowDialog () method creates another problem. Let's say I want to get rid of the old form, for example:

 public void switchForm(Form newForm) { Form oldForm = currentForm; currentForm = newForm; newForm.ShowDialog(); oldForm.Dispose(); } 

Then I found out that the code of oldForm.Dispose () is not executed until the execution of newForm.ShowDialog () is complete (the form is closed). Therefore, the foregoing can lead to leaks when the forms are not correctly placed, since the method is called again and again during the transition to new forms. Another way is to first discard the old form before showing the new one, then my application temporarily displays something else (regardless of the form) between the old form that will be deleted and the new rendered: / How should I go? something like this?

Many thanks.

+4
source share
2 answers

Try this :

 public void switchForm(Form newForm) { Form oldForm = currentForm newForm.Show(); currentForm.Hide(); currentForm = newForm; oldForm.Dispose(); } 
+2
source

Each form that you create in a project is a class.
For instance:
I am creating a form called frmExample . If you want to call it from another form, you need to do the following:
-Create a new instance of the class (your form) that you need
-Install a new instance (modal or non-modal)

 Class frmOther<br> Dim frmNewForm as New frmExample()<br> frmNewForm.Show()<br> End Class 
+1
source

Source: https://habr.com/ru/post/1310084/


All Articles