|
-
[RESOLVED] Bypass FormClosing
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:
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
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.
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.:
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
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.
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.
-
Re: Bypass FormClosing
No useful suggestions here, but this unlocked a memory from ages ago when I was starting out as an IT Tech at my first real job back in 2000.
One department in the company I worked for used a piece of software that apparently also suffered from the "things don't flow correctly if the user clicks the X button in the upper-right" issue. The solution the developer of that program came up with was to have a tiny, "do-nothing" window that was designated as "always on top" displayed in the upper-right corner of the screen, so that it covered up the "X" button.
-
Re: Bypass FormClosing
You can disable the X in your code…
I’ll find it for you…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Bypass FormClosing
This is the code to disable the X...
Code:
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Bypass FormClosing
BTW, I can only post via mobile safari now. This must be having a huge impact on the site if this is happening to others too…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Bypass FormClosing
Thanks, I will go with the CreateParams overload.
FYI - I'm currently on Edge with no problems.
FYI #2 - I cannot rep either of y'all but thank you nonetheless for the input.
-
Re: [RESOLVED] Bypass FormClosing
Could this be a win10/win11 issue? I’m running win10
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: [RESOLVED] Bypass FormClosing
The problems I’m experiencing appear to be related to http requests being corrupted…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: [RESOLVED] Bypass FormClosing
Definitely open this up in the forum support. I want Steve on this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|