Handling Edit Cancellations


The last thing you have to cover is rolling back your objects' state if a user cancels an edit. The main reason to do this is for consistency. Your project standard is to load an individual record from the database every time the user wants to edit a record. This, in part, negates the need to roll back changes. However, it is never a good idea to leave an object in an invalid state. After every single edit in the user interface, a property is set in your user-centric objects. If the user cancels their actions, the object will not be valid because the information will not match the data in the database.

Chapter 3, "Creating the Application Infrastructure," mentioned that the purpose of the msRegion variable in the Region class was going to be used to help maintain object state in case the user clicked a Cancel button. Here you will implement that functionality, which is surprisingly simple. Add the following new method to the Region class in the Region.vb module:

 Public Sub Rollback()      LoadObject() End Sub 

This method calls the LoadObject method, which loads the class members from the module-level structure. This returns the object to the state it was in before the edit began.

Finally, you need to make sure users do not accidentally lose any changes. Also, if they do want to lose their changes, you need to roll back the changes by calling the Rollback method of your Region object. To do this, add the code in Listing 5-13 to the frmRegionEdit form.

Listing 5-13: The frmRegionEdit_Closing Method

start example
 Private Sub frmRegionEdit_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing      Dim dlgResult As DialogResult      Try           If mobjRegion.IsDirty Then                dlgResult = MessageBox.Show("The Region information has " _                & "changed, do you want to exit without saving your changes?", _                "Confirm Cancel", MessageBoxButtons.YesNo, _                MessageBoxIcon.Question)                If dlgResult = DialogResult.No Then                     e.Cancel = True                Else                     mobjRegion.Rollback()                End If           End If      Catch exc As Exception           LogException(exc)      End Try End Sub 
end example

This code checks to see if the object is dirty (if the user clicked OK to close the form, the dirty flag is set back to false after the change). If it is dirty, you ask the user if they really want to exit and lose their changes. If they answer no, you set the Cancel property for the form to True, which stops the form from closing. If they do want to lose their changes, you call the Rollback method. That is all there is to it.

Note

You may want to ask the user if they want to save their changes before they exit the form and give them the option of clicking Yes, No, or Cancel. You can do this, but it is just a bit more involved. To do it, you need to call the code in the btnOK_Click method, check to see if there were any business errors from the data-centric object, and then cancel the close if there were and close the form if not.




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net