|
-
Apr 3rd, 2011, 03:25 PM
#1
Thread Starter
Junior Member
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.
-
Apr 3rd, 2011, 10:06 PM
#2
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:
Select Case MessageBox.Show("...")
-
Apr 4th, 2011, 12:27 PM
#3
Thread Starter
Junior Member
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
|