Results 1 to 6 of 6

Thread: Closing the App

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    Closing the App

    This probably has a simple explaination, but it simply evades me at the moment. When I am trying to close
    my application, I use Application.Exit, but the problem is that when it reaches this statement, it then goes thru
    the FrmMain_FormClosing a second time, so if you watch Static count, it displays the messagebox indicating that
    the formClosing event has run twice. I have boldened the most relevant code. It has been so long since I have
    dealt with this type of code, I just do not remember how to get it working correctly!

    Code:
        Private Sub FrmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim FHS As New AppFileHistory
    
            Try
                ' If no text in textbox, close application
                If Me.RTBMain.Text = "" Then
                    System.Windows.Forms.Application.Exit()
                    End
                    Exit Sub
                End If
                ' Determine if text has changed in the textbox by comparing to original text.
                If (Me.RTBMain.Text <> strMyOriginalText) Or Properties.pBoolRTBModified = True Then
                    ' Display a MsgBox asking the user to save changes or abort.
                    messageboxYesNoCancelShown = True
                    Select Case MsgBox("Do you want to save changes to your text?", MsgBoxStyle.YesNoCancel)
                        Case MsgBoxResult.Yes
                            Cursor.Current = Cursors.WaitCursor
                            If Properties.pStrCurrentFilePath.Length > 0 Then
                                Me.btnSave.PerformClick()
                            Else
                                ToolStripMenuItemSaveAs_Click(Me, e)
                                Exit Sub
                            End If
                            Cursor.Current = Cursors.Default
                        Case MsgBoxResult.Cancel
                            Exit Sub
                        Case MsgBoxResult.No
                            Exit Sub
                    End Select
                End If
    
                '  DO other things
    
                Static count As Integer
                count = count + 1
                If count > 1 Then
                    MsgBox("Double trouble . . . ")
                End If
    
                Application.Exit()
                End
    
            Catch ex As Exception
                FIO.reportError("Error:" & ex.StackTrace & ". . . ." & vbCrLf & Err.Description & vbCrLf & " Exception:  " & ex.ToString)
            End Try
        End Sub
    
        Private Sub btnClose_Click(sender As Object, e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub
    .
    We must question the story logic of having an all-knowing all-powerful God, who creates faulty Humans, and then blames them for his own mistakes.
    GENE RODDENBERRY
    .
    http://www.tachufind.com
    .

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Closing the App

    If the form's closing, why do you need any other exit? By default, closing the start-up form exits the application.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2009
    Location
    Arizona
    Posts
    185

    Re: Closing the App

    The same thing happens without it . . . I just happened to leave it that
    way after trying various things.

    I found one thing that solves the issue, but it is not pretty:

    Static count As Integer
    count = count + 1
    If count > 1 Then
    End
    End If

    I think there is probably a better solution than that . . .
    .
    We must question the story logic of having an all-knowing all-powerful God, who creates faulty Humans, and then blames them for his own mistakes.
    GENE RODDENBERRY
    .
    http://www.tachufind.com
    .

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Closing the App

    Well it probably doesn't help that you've got message boxes and a lot of other stuff going on. Why can't the bulk of this (if not all of it) be done in the btnClose click before the Close is called?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Closing the App

    If the form is closing anyways, then why would you need to tell it to close if that's what it's already doing? That's the issue here, telling it to close when it's already in the process of closing. Although it doesnt' really do a lot of good anyways regardless of the issue at hand here, unless you've got multiple conditions to be evaluated; setting the e.Cancel property, etc... I don't see it though lol.

    What's the goal here?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Closing the App

    The contents of a FormClosing event handler should take this general form:
    Code:
    If ShouldTheFormStayOpen Then
        e.Cancel = True
    End If
    That's basically it. As has already been said, the FormClosing event is raised because the form is closing, so there's no need for you to do anything else to make it close. You only have to do something if you don't want the form to close, i.e. set e.Cancel to True. You can do whatever else you like in there as well but NOTHING else related to closing or not closing the form. To be more specific to your case:
    Code:
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If TextBox1.TextLength > 0 Then
            Select Case MessageBox.Show("Would you like to save your changes?",
                                        "Changes Pending",
                                        MessageBoxButtons.YesNoCancel,
                                        MessageBoxIcon.Question)
                Case DialogResult.Yes
                    'Save the changes and let the form close.
                    Button1.PerformClick()
                Case DialogResult.No
                    'Do nothing.
                Case DialogResult.Cancel
                    'Don't let the form close.
                    e.Cancel = True
            End Select
        End If
    End Sub
    That's basically it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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