I am using Compact Framework 3.5 on a Symbol MC50.
In my Sub Main it first checks to see if a database exists. If it does, it shows a login screen using the following code:
Dim login As frmLogin = New frmLogin()
If login.ShowDialog() = DialogResult.OK Then
Application.Run(New frmMain())
End If
This all works correctly and, when I close frmMain, it exits the application as expected.
However, if the database check in Sub Main fails, I call the ShowDialog() method of another form that is used to create and fill the database from a live server. Here is the code for calling this form:
If Not File.Exists(SETTINGS_LOCALDB) Then
databaseExists = False
MessageBox.Show("Local DB does not exist. The database must be created before using the application.")
Dim update As frmUpdateData = New frmUpdateData()
update.ShowDialog()
Else
.....
End If
The first problem that I had is that when frmUpdateData was closed the rest of the code in Sub Main was not executing, thus the Application.Run was never being hit.
So in the click event of the Close button on frmUpdateData I added the following code:
If SystemUserSecurityId() = Nothing Then
Dim login As frmLogin = New frmLogin()
If login.ShowDialog() = DialogResult.OK Then
DebugTrace("Init - login complete, starting application.")
Application.Run(New frmMain())
End If
End If
Me.Hide()
All of this code is being hit and frmMain does indeed load. However, when I click the close button in the top right nothing happens, no event occurs. Its as though the Windows events are not occurring.
What have I done wrong?
The reason your main form won’t close is because your application is “stuck” in the callstack of the closing event of your dialog, as that’s where you started the windows message loop for your main form.
I would suggest your restructure your code somewhat.
Instead of doing your validity checks in the “main” sub, load the main form instead:
Drop a timer onto your main form and set a very quick interval (say 10 ms). Enable it in the main form’s load event. Implement the Tick event handler like so (note that my VB syntax probably isn’t perfect, I’m winging it here):