Question Function to load and dispose forms

giovaniluigi

Member
Joined
Jan 9, 2011
Messages
16
Programming Experience
5-10
I'm using .NET compact framework and I want to do a function or sub to manage the forms of my application.

The idea is to replace, for example, the code below:

Inside FormA:


Code:
FormA.Enabled = False
frmWait.show()
frmWait.TopMost = True
Dim f as new FormB
f.show()
f.TopMost = True
frmWait.hide()
Me.close

The code above is called from FormA to load FormB
While FormB is loaded, FormA is disabled to prevent user to interact with it,
The frmWait is displayed to show to user a bitmap for example to be patient...
(frmWait is already loaded in memory, so it should be displayed without delay. I loaded it in Main.vb)
While FormB is loaded into memory (which takes 5 seconds) frmWait is on top and being displayed.
When FormB is loaded it is displayed and frmWait is hided.

Well this is needed because I'm running in a slow ARM CPU with WinCE 6 forms with a lot of controls on them.

The problem is that I have a lot of forms in my project.
I want to manage all this from a function/sub inside Main.vb
This way I will avoid to write the same code a lot of times...
Also I will have an elegant way to manage them.

I want that function or sub do something like:

Called from FormA, passing FormB class to create an instance and continue from it:
It should block user interaction with FormA
It should display frmWait
It should load FormB
It should display FormB
FormB should be displayed as TopMost
It should close FormA
The application now should continue to run on FormB.

The code should be something like that:

Code:
Public Sub GoToForm( ByRef MyForm as Form, ByRef NewForm as Form )
[INDENT]
MyForm.Enabled = False
frmWait.Show()
MyForm.Close()
Dim f As New NewForm
f.Show()
f.TopMost = True
Application.Run(f)
[/INDENT]
End Sub
The main problem that I can see here is:

How to tell to the function what form to load ? I mean, how to pass a form type as a parameter ?
How to change application to run from FormB after closing FormA ? I think that Application.Run(f) may be not the right thing...

These are my questions. Remembering that this is inside Main.vb the StartUp point of my project.
 

aeskan

Well-known member
Joined
Aug 10, 2011
Messages
63
Programming Experience
3-5
Instead of Enabling and Disabling and TopMost your forms, use MyForm.ShowDialog and it will do all together more simple and better. Just beware after the first use of this method, VB.Net will automatically choose one of the Form's buttons as Cancel Button and another as Accept Button, if so, you could return those buttons to their defaults for once and it would not change again. And remember after closing any form use MyForm = Nothing at the end (if there is no data exchange between your forms or after all). Hope that it will help you.
 
Last edited:

giovaniluigi

Member
Joined
Jan 9, 2011
Messages
16
Programming Experience
5-10
Well, my idea is not showDialog and return to the same form.

Think this way:

I have 40 forms in my application.
For example, I will start application then load a form called frmMenu.
This form can call frmSettings, frmHelp or frmNew.
All of those other forms can create another forms too.

I can't left all those 40 forms loaded in ram. (Max. 3 forms in RAM)
All forms are topmost and fullscreen. It is an application without floating windows.
All full screen.

Imagine that project like a website navigation.
I want a sub/function to navigate through 40 WinForms, loading and unloading, leaving the control with the actual form.

Someone ?
 

aeskan

Well-known member
Joined
Aug 10, 2011
Messages
63
Programming Experience
3-5
You should use MDI Child forms instead of normal forms to navigate like a web site, you will have appropriate methods then! Other solution probably is to use TabPages.
 

giovaniluigi

Member
Joined
Jan 9, 2011
Messages
16
Programming Experience
5-10
In .NET CF there is no MDI form.
Tabs are not ideal for that kind of application.
Some forms have no relation with others so arranging in tabs would be weird...
Also they are already designed.

Must have some way to manage them with a sub in Main.vb.
I'm just with some problems to pass the right parameters to the sub/function.
I need to inform what form to load.
But I need to pass the type otherwise will be impossible to assign the form to a variable from unknown type.
 

aeskan

Well-known member
Joined
Aug 10, 2011
Messages
63
Programming Experience
3-5
Guess you need to first declare Enumeration to define your types, and then use those types in your desired sub or function. Same as bellow:

Enum EnumFormType
Form1 = 1
Form2 = 2
Form3 = 3
.....
End Enum


Public Sub GoToForm(ByRef MyForm As Form, ByRef NewForm As Form, ByVal FormNumber As EnumFormType)
Select Case FormNumber
Case 1
MyForm.......
Form1.Show
Case 2
MyForm.......
Form2.Show
......
End Select
End Sub


Need more information about your algorithm and your purpose.
 

giovaniluigi

Member
Joined
Jan 9, 2011
Messages
16
Programming Experience
5-10
Yes, that should work.
But I also would like to know if there is another way to avoid this code.
Since every form is a class it should be possible to pass the class name as a parameter of function.
The enum makes you need to add every form.
But I'm thinking in a "smarter" way than enum...
I will wait for more suggestions, otherwise I should use enum.
 
Top Bottom