Results 1 to 8 of 8

Thread: Function to load and dispose forms

  1. #1
    giovaniluigi is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jan 2011
    Posts
    16
    Reputation
    31

    Cool Function to load and dispose forms

    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 )
    
    MyForm.Enabled = False frmWait.Show() MyForm.Close() Dim f As New NewForm f.Show() f.TopMost = True Application.Run(f)
    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.

  2. #2
    aeskan is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Posts
    54
    Reputation
    25
    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 by aeskan; 06-11-2012 at 5:04 PM.

  3. #3
    giovaniluigi is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jan 2011
    Posts
    16
    Reputation
    31
    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 ?

  4. #4
    aeskan is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Posts
    54
    Reputation
    25
    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.

  5. #5
    giovaniluigi is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jan 2011
    Posts
    16
    Reputation
    31
    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.

  6. #6
    aeskan is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Posts
    54
    Reputation
    25
    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:

    EnumEnumFormType
    Form1 = 1
    Form2 = 2
    Form3 = 3
    .....
    EndEnum


    PublicSub GoToForm(ByRef MyForm AsForm, ByRef NewForm AsForm, ByVal FormNumber AsEnumFormType)
    SelectCase FormNumber
    Case 1
    MyForm.......
    Form1.Show
    Case 2
    MyForm.......
    Form2.Show
    ......
    EndSelect
    EndSub


    Need more information about your algorithm and your purpose.

  7. #7
    giovaniluigi is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Jan 2011
    Posts
    16
    Reputation
    31
    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.

  8. #8
    aeskan is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Posts
    54
    Reputation
    25
    There is no reason to add every form, you can call its class or every other thing. If you make a class with get and set functions also you can exchange your parameters to the class.

    Also can see http://msdn.microsoft.com/en-us/library/aa446546.aspx and http://www.computerbooksonline.com/c...asp?a=3179&z=4
    Last edited by aeskan; 06-11-2012 at 8:55 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking