Form Switching

Hello all, I know this is a real n00b question, but I was wondering if someone could advise me as to a better way of doing my form switching without creating a new instance of a form each time.

I know usually you can just go

Dim F as new Form2
F.Show

But that creates a new instance of Form2. What I'm trying to do is to have a permanently open version of Form2 that I can access at any time. The reason I want to do this is so that I can keep all the information stored on that form (text box contents, etc) available for use later if the form is closed and re-opened again.

I found a method to do this, which was to create a module and put in the line:

Public F as new Form2

But this causes some serious problems when I open and close that form a few times using .Hide and .Show commands respectively.

So if anyone knows of a better way I could be doing this so I can have a permanent instance of a form that I can use over and over again, or perhaps just a better way of opening and closing a reference made using the processes I am using now, that would be much appreciated.

Thanks in advance.




Answer this question

Form Switching

  • Igal Chariski

    Also, I cannot seem to get your "My" method working. It always comes up as invalid syntax.

  • CMeister

    VB 2005 / Express makes something like this very simple and much more robust.

    You should definately download vb express and give it a try (its free) and you can see a lot of the new features.


  • brianpmccullough

    i can see potential problems with the code youve shown

    There is still nothing to stop creating second instances of forms.

    The variables in the form arent type specific - i could store any form in any of these variables.

    The general concept of the variables in a module globally accessible within you application is really what the my.forms collection in express/2005 is all about but this collection means that you only have a single instance of a given type and that when you add a new form type to your project you dont need to add another variable to a module.

    Also the practice of simply hiding the forms rather than closing then - what happens if the user actually wants to close them like when you close an application.   instead of simply creating a form, you need to check the reference is set an if it is make it visible, if it isnt create a new instance then show it and set the module variable, so two different conditions as to form creation/visibility.

    great if this code works for you, but it does have some limitations.

     


  • MeMyselfNDotnet

    I'll do that. Thanks for the advice.

  • CDFITGUY

    Ahh, ok. This is the problem then I guess, as I'm using VB.NET2003... I'm old-school lol

    Thanks for your contribution. Even though the code itself didn't answer my query, the theory behind it led me to write mine, so thank you very much for your help.



  • VCBeginer

    Thank you for the response. If I was to use your second example to define a reference variable, is there any way I can set that up to be public across the entire project so that multiple forms can access multiple other forms, instead of having to use a single form as a base for opening everything

  • sk78

    The way you can do this is with a default instance form and the My.Forms collections.

    So you would refer to a for as

    My.Forms.<Form Class Name>

    and this will refere to a single default instance.

    Then you can say

    My.Forms.Form2.Text = "foo"
    My.Forms.Form2.Show

    If you want to define a reference variable you can use something like

    Dim x as Form2
    X= my.Forms.Form2
    With X
    .text = "foo"
    .show
    End With

    With this you dont have to bother about instancing the forms - there will only be one instance of my.Forms.Form2 in the application.


  • Smola

    you are using vb express or 2005, this is a new feature in this version. if your not then you'll need to have to do a bit of work to in essence create a forms collection.


  • VolcanoDan

    Well, I've found the solution. Simply create a module declaring each variable name you wish to use for each form you will reference, like so:

    Module Module1
    Public X As Form
    Public Y As Form
    Public Z As Form
    End Module

    Then, in the load event of the first form loaded in your project, populate these variables with the forms you will be referencing, like so:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load

    X = New Form1
    Y = New Form2
    Z = New Form3

    End Sub

    Then, when you wish to reference an aspect of any of those forms, you simply use that reference variable.

    However, to prevent your forms from disposing, you must stop the close event and replace it with a Hide event. You do this in the Form_Closing event, like so:

    Private Sub Form2_Closing(ByVal sender As Object, ByVal e As _
    System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    e.Cancel = True

    Me.Hide()

    End Sub

    I hope this helps someone.



  • Form Switching