I have a WinForm project targeting .NET Framework 4.5 with two forms: FormStartup and FormMainMenu.
The project's startup form is FormMainMenu and the shutdown mode is set for when the startup form closes. Inside of my application events, I have this:
The startup form uses the current identity principal user's name (displayed/read-only), but the user can select which company they're using the application for and when they click on the login button it makes a database call to verify that they have access.Code:Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup Using startupFormDialog As New FormStartup() e.Cancel = startupFormDialog.ShowDialog() <> DialogResult.OK End Using End Sub
From the FormMainMenu, there's a ToolStripButton that allows them to change the company which should show the FormStartup again. Separately, there's an "Exit Program" button that allows them to simply close the application.
Since the FormMainMenu should only be visible after successfully going through FormStartup, I was thinking about taking this approach:
- Have ButtonExitProgram button call the Application.Exit method to simply close the application
- Have ToolStripButtonBackToStartup call the Form.Close method to start the process of closing the application
- Do the same business logic that exists in my Application.Startup event inside the FormClosing event
- The cancel logic would be inverted to check equals to instead of does not equal
E.g.:
The problem is that while everything works like it should, if I click on the red X in the title bar of the form, then it still goes through my FormClosing logic.Code:Private Sub FormMainMenu_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing Hide() Using startupFormDialog As New FormStartup() e.Cancel = startupFormDialog.ShowDialog() = DialogResult.OK End Using Show() End Sub Private Sub ToolStripButtonBackToStartup_Click(sender As Object, e As EventArgs) Handles ToolStripButtonBackToStartup.Click Close() End Sub Private Sub ButtonExitProgram_Click(sender As Object, e As EventArgs) Handles ButtonExitProgram.Click Application.Exit() End Sub
I could setup a Boolean check and set it in the ToolStripButtonBackToStartup's click event, but that feels like a hack. Before I went that route, I wanted to check here to see if there was any way to distinguish between programmatically calling the Close method and when the user clicks on the built-in close button in the Form's title bar.




Reply With Quote