Results 1 to 9 of 9

Thread: [RESOLVED] Bypass FormClosing

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,406

    Resolved [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:
    1. Have ButtonExitProgram button call the Application.Exit method to simply close the application
    2. Have ToolStripButtonBackToStartup call the Form.Close method to start the process of closing the application
    3. Do the same business logic that exists in my Application.Startup event inside the FormClosing event
      1. 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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,638

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,431

    Re: Bypass FormClosing

    You can disable the X in your code…
    I’ll find it for you…

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,431

    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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,431

    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…

  6. #6

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,406

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,431

    Re: [RESOLVED] Bypass FormClosing

    Could this be a win10/win11 issue? I’m running win10

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,431

    Re: [RESOLVED] Bypass FormClosing

    The problems I’m experiencing appear to be related to http requests being corrupted…

  9. #9

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,406

    Re: [RESOLVED] Bypass FormClosing

    Definitely open this up in the forum support. I want Steve on this.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width