Results 1 to 3 of 3

Thread: Checking for changes before closing (HasChanges)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2010
    Posts
    31

    Checking for changes before closing (HasChanges)

    I'm having trouble using the HasChanges method.

    I'm trying to get a message box to show if a user hits the edit button, makes a change, then hits the close button without hitting the save button first. From what I understand, the HasChanges(DataRowState.Modified) statement should work.

    In reality, when I hit the edit button, make a change to a field (zip for example), then hit the close, it just closes without showing the messagebox.

    Here's my code so far...
    Code:
        Private Sub CustomerForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles Me.FormClosing
    
            'check for unsaved changes
            Dim AnswerDialogResult As DialogResult
    
            If aCarsDataSet.HasChanges(DataRowState.Modified) Then
                'query user to save changes
                AnswerDialogResult = MessageBox.Show("Do you want to save the changes?", "Unsaved Changes", _
                                                     MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, _
                                                     MessageBoxDefaultButton.Button2)
                Select Case DialogResult.Yes
                    Case Windows.Forms.DialogResult.Yes
                        'save dataset
                        aCustomerBindingSource.EndEdit()
                        aCustomerTableAdapter.Update(aCarsDataSet)
                        'cancel closing
                        e.Cancel = True
                End Select
            End If
    
            AnInstance = Nothing
        End Sub
    Not really sure why this doesn't ever get triggered. Slowly running out of ideas.

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

    Re: Checking for changes before closing (HasChanges)

    Are you calling BeginEdit when the user clicks the Button? You aren't calling EndEdit until after you call HasChanges so, in that case, there are no committed changes when you call HasChanges.

    Also, your Select Case is wrong. You're testing the value of DialogResult.Yes, which is always going to be DialogResult.Yes. You want to test the value returned MessageBox.Show, which is in your AnswerDialogResult variable.

    Further to that, there's no point declaring AnswerDialogResult outside the If block if it's only going to get used inside the If block. If the condition tested is False then you've allocated space for that variable for no reason.

    Also, while it doesn't hurt exactly, the variable itself is unnecessary. You can simply test the result of MessageBox.Show directly:
    vb.net Code:
    1. Select Case MessageBox.Show("...")
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2010
    Posts
    31

    Re: Checking for changes before closing (HasChanges)

    Thanks for all the info!

    How would I implement the beginedit method?

    I have 2 books that I'm looking through and both seem to just gloss over that.

    I assume that it goes into the editButton_click sub.

    EDIT: Nevermind. I think I got it figured out!
    Last edited by neveser; Apr 4th, 2011 at 02:12 PM.

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