Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection of open forms?

Status
Not open for further replies.

Billkamm

Programmer
Feb 9, 2006
74
US
Is there a collection that will contain all of the open forms in VB.Net?

I have two forms FormA and FormB. FormB opens on the start of the program (as a splash screen) and then it using ShowDialog to display FormA. FormA however takes awhile to load because it is pulling A LOT of information from a database, so I want FormB to stay active until FormA's Load stage is finished. At the end of FormA's load stage I want to close FormB. How can I refer to FormB (which loaded FormA) from FormA's load event, so that I can close it?
 
That's not possible the way you are doing it now. If you load FormB and open FormA with showdialog, then FormB is the parent of FormA and when you manage to close it, FormA will also be closed.

A better way to achieve this is by the use of a module in your project, containing a Sub Main().

It would look something like this:

Code:
Public Module mdlStartup
  Sub Main()
    'Declare a new instance of the splash screen
    Dim frmB As New FormB()
    'Show the splashscreen non-modal
    frmB.Show()
    'Load the main form
    Dim frmA As New FormA()
    'After the main form has loaded, close the splash screen
    If Not frmB Is Nothing Then
      frmB.Dispose()
    End If
    'Show the main form modal
    frmA.ShowDialog()
    'If the main form is closed, dispose it if it still exists
    If Not frmA Is Nothing Then
      frmA.Dispose()
    End If
  End Sub
End Module

There are some important things to watch here:

1) The startup object of you project must be set to 'Sub Main'
2) The loading of data on your FormA must be started from the 'Sub New' of your form. Example:

Code:
Public Sub New()
  MyBase.New()

  'This call is required by the Windows Form Designer.
  InitializeComponent()

  'Add any initialization after the InitializeComponent() call
  LoadData()
End Sub

3) As you can see above, FormB must be shown using .Show() and FormA must be shown using .ShowDialog()
The reason for this is that if you show FormB with .ShowDialog() the loading of FormA will not be executed untill you manually close FormB. If you show FormA with .Show() then the code in the module will immediately jump to the next line of code after showing FormA, which will then dispose of it.

Regards, Ruffnekk
---
Do not underestimate the power of simplicity!
 
module in your project

Never in a million years.

in the form that needs a long tiime to load

Code:
 Dim frmsplashscreen as frmsplash

Public Sub New()
  MyBase.New()

  'This call is required by the Windows Form Designer.
  InitializeComponent()

  'Add any initialization after the InitializeComponent() call
 frmsplashscreen.show
application.doevents
End Sub

and then in the load of that form.

Code:
'start long running code
'when finished
frmsplashscreen.close
frmsplashscreen = nothing

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top