Results 1 to 15 of 15

Thread: [RESOLVED] Form Closing Check if file saved

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Resolved [RESOLVED] Form Closing Check if file saved

    I am trying to add a custom for closing statement but have a snag
    I want to make sure the users work is saved before closing so i have if the mstrFileName string is empty then to prompt user to save. But it only works if the user starts a brand new form then closes without saving. IF a user edits an existing file (the mstrFileName is in the string so) it is not prompting the user to save work.

    Code:
        Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If mstrFileName = String.Empty Then
                Dim response As MsgBoxResult
                response = MsgBox("Close Without Saving?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
                If response = MsgBoxResult.Yes Then
                    Me.Dispose()
                ElseIf response = MsgBoxResult.No Then
                    e.Cancel = True
                    If mstrFileName = String.Empty Then ' it is new and never saved before, show Save dialog
                        SaveAs()
                    Else ' save immediately
                        Save()
                    End If
                End If
            End If
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Form Closing Check if file saved

    If the file name field can't do the job then just don't use the file name field. Do the obvious: just declare a field specifically for the purpose. Set it to False when there's new data to save and set it to True when it's saved.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Form Closing Check if file saved

    By the way, get rid of that Dispose call. If the FormClosing event is raised then the form is already closing and you don't have to do anything to make it close. You only have to do something if you want it to NOT close.

    I would also suggest that your message box should be using YesNoCancel buttons. Yes means save and close the form, No means close the form without saving and Cancel means don't close the form.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    Quote Originally Posted by jmcilhinney View Post
    By the way, get rid of that Dispose call. If the FormClosing event is raised then the form is already closing and you don't have to do anything to make it close. You only have to do something if you want it to NOT close.

    I would also suggest that your message box should be using YesNoCancel buttons. Yes means save and close the form, No means close the form without saving and Cancel means don't close the form.
    Ok thank you that makes sense. I'll post my results

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Form Closing Check if file saved

    Can you provide a little more detail. Explain this "work" the user is doing. Surely you can set a flag if the form has been modified.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    OK so i have now a global boolean to check for if a row in the dgv is added or changed it sets this boolean to true and then in the closing event it checks to see if the boolean is set to true and it is all working except if i delete a row form the dgv it doesnt seem to set the boolean or something.
    Code:
        Private Sub DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            BindingContext(mDataTable).RemoveAt(BindingContext(mDataTable).Position)
            dChanged = True 'my boolean
    
        End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Form Closing Check if file saved

    Quote Originally Posted by compgeek1979 View Post
    OK so i have now a global boolean to check for if a row in the dgv is added or changed it sets this boolean to true and then in the closing event it checks to see if the boolean is set to true and it is all working except if i delete a row form the dgv it doesnt seem to set the boolean or something.
    Code:
        Private Sub DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            BindingContext(mDataTable).RemoveAt(BindingContext(mDataTable).Position)
            dChanged = True 'my boolean
    
        End Sub
    That method has no Handles clause so I'm guessing that it's not actually handling any event.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    ok so what can I do to address this? I need that if a row is removed then it sets this boolean to true

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Form Closing Check if file saved

    Quote Originally Posted by compgeek1979 View Post
    ok so what can I do to address this? I need that if a row is removed then it sets this boolean to true
    Um, I said that it's missing a Handles clause... so add an appropriate Handles clause. Do you know what a Handles clause is? Have you bothered to compare that method to your event handlers that are working?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    You know, I don't understand you, you post so much and are a very smart person, but why you insist on insulting me (if not others) is beyond my comprehension. I am learning as I go and no i don't know a whole lot, that's what forums are for. An explanation or example would be very helpful. I know you get frustrated with people who don't try and don't research, that is understandable. I however look for answers before posting and try to make sense of what I find. So yes I have looked at my code that does work. I have googled and found many things, most of which i cant make sense of because its only snippets. I don't understand what you are getting at because to my understanding ( I could be wrong)
    Code:
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
    ExitToolTripMenuItem.Click is the back end event and ExitToolStripMenuItem_Click tells the back end what to do. That's the best i can explain it.
    However if you are inclined to insult me again please do not reply. I am looking for assistance, not someone to tell me I am stupid or not trying. Thank you

  11. #11
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Form Closing Check if file saved

    There was a "check if there is undo" method or something smilar in excel vba some years ago. I'm sure there still is
    What I am not sure is if vba was checking excel's undo list or was it really tracking the actions ...
    Last edited by Flashbond; Nov 7th, 2013 at 09:24 AM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Form Closing Check if file saved

    You got an example right in front of you. Just open your eyes and look at it. I said that this:
    Code:
    Private Sub DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    is missing a Handles clause. Look at your own code from post #10:
    Code:
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
    I said compare to your other event handlers that were working and you obviously didn't. You also obviously didn't make any effort to find out what a Handles clause is, yet you also didn't actually say "I don't know what a Handles clause is". These are not insults. These are facts. You're not doing much to help yourself, in this thread or others. I'm done.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    Quote Originally Posted by jmcilhinney View Post
    You got an example right in front of you. Just open your eyes and look at it. I said that this:
    Code:
    Private Sub DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    is missing a Handles clause. Look at your own code from post #10:
    Code:
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
    I said compare to your other event handlers that were working and you obviously didn't. You also obviously didn't make any effort to find out what a Handles clause is, yet you also didn't actually say "I don't know what a Handles clause is". These are not insults. These are facts. You're not doing much to help yourself, in this thread or others. I'm done.
    I change the code ( Sorry I did not post the change) and it still does not work
    Code:
    Private Sub DelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelRow.click
    "Error: Handles clause requires a WithEvents variable defined in the containing type or one of its base types."

    I am sorry if feel i did not do what you asked. I implicated that my understanding of a handles clause may not be correct, hoping that if i am wrong that you would correct me.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Form Closing Check if file saved

    Anyone?

  15. #15
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [RESOLVED] Form Closing Check if file saved

    There's really nothing complicated about this.. if the form is closing ask the user (only if there have been changes to the data):
    vb Code:
    1. Public Class MainForm
    2.  
    3.     Private m_DataNeedsSaving As Boolean
    4.  
    5.     Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    6.         If m_DataNeedsSaving Then
    7.             If e.CloseReason <> CloseReason.TaskManagerClosing AndAlso e.CloseReason <> CloseReason.WindowsShutDown Then
    8.                 Select Case MessageBox.Show("Save your data?", "Save Data", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
    9.                     Case DialogResult.Yes
    10.                         'Save the data
    11.                     Case DialogResult.Cancel
    12.                         'Cancel the form closing
    13.                         e.Cancel = True
    14.                 End Select
    15.             Else
    16.                 'System is shutting down or being forced to quit, don't give them a cancel option
    17.                 If MessageBox.Show("Save your data?", "Save Data", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
    18.                     'Save the data
    19.                 End If
    20.             End If
    21.         End If
    22.     End Sub
    23.  
    24. End Class
    I'll let you sort out when to set the m_DataNeedsSaving variable to True/False
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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