Herfried,
I declare F0 the same way as I declare the other forms, it is initially asigned to frmMain in the Sub Main as follows:
Public Sub Main()
F0 = New frmMain
Application.Run(F0) ' The main sub will halt execution here until F0 is closed.
call EndApplication 'This routine closes some global stuff and generally tidies up.
End Sub
In the Form_Load of the frmMain (now referred to as F0) I open my start child form:
Dim Resp As Boolean
'Stuff that happens only when the form first loads.
Try
Resp = ShowPage(Common.AppScreens.StartScreen)
'Get User Settings from Registry
If Not GetUserSettings(0) Then
ShowPage(Common.AppScreens.Options)
End If
Catch ex As Exception
Call ProgErrorHandler("frmMain_Load", "frmMain", ex.Message, False)
End Try
My routine "ShowPage" is a module level function that opens the forms. "AppScreens" is an enum for each form, I originally used an array var F() to hold the references to the forms, but found that I couldn't expose the controls as I wanted to without individually declaring each var as a type of the actual form rather than just using the Form object itself. The original code was along the lines of:
Dim F(20) as form
Then in the ShowPage routine below it did a:
F(2) = new frmSearch
F(2).MdiParent = F(0)
F(2).show
This worked fine until I tried to reference cotrols on F(0) the MDI Parent and kept getting errors like "sbp1 is not a member of System.Windows.Form" or something similar (sbp1 being a panel of the StatusBar that I wanted to write some text to). I eventually realised that assigning each form to an array of form was limiting me to only being able to access objects exposed by the form object, not a true instance of the actual frmSearch or any other forms in my app. (I am an old VB6'er and this strict typeing is hard to adjust to). When I twigged (with some help from this forum) why I couldn't modify controls on the F(0) (MDI Parent form) I changed it to the code below. At the declarations of the module I define each form variable as:
Friend F0 as frmMain
Friend F1 as frmStart
Friend F2 as frmSearch etc ..
Then when I want to open one of them I call the ShowPage routine, I first assume the form is already open and try to just open it. If this fails because the F2 F3 F4 etc var is not allocated, the Catch part then opens it.
Public Function ShowPage(ByVal pageToShow As AppScreens, Optional ByVal CreateNewCustomerRecord As Boolean = False) As Boolean
Try
'Try and show the page assumes it already exists
Select Case pageToShow
Case AppScreens.StartScreen
F1.Show()
F1.Activate()
Case AppScreens.SearchScreen
F2.Show()
F2.Activate()
End Select
'If not then create it after exception occurs
Catch ex As Exception
Select Case pageToShow
Case AppScreens.StartScreen
F1 = New frmStart
F1.MdiParent = F0
F1.Show()
Case AppScreens.SearchScreen
F2 = New frmSearch
F2.MdiParent = F0
F2.Show()
End Select
End Try
ShowPage = True
End Function
The problem is however that if the user clicks the inner close box and closes the child form or if I programmatically close it from a button press or whatever, the code is called to do the f2.close() and f2.dispose() but then when I want to open that form again in future the code to open in the "Try" part of the "ShowPsge" just passes through without raising an error, so the Catch never gets called to open it again??
It all seems a bit weird??
Siv