Save question

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
I have an application that i want to when closing prompt the user with the question if they want to save changes yes or no. And if yes save the changes. However if the user just opened the application and pulled up a certain record, but made no changes and begun to close the application, I do not want it to prompt them with that question. So if they made changes prompt them with the question if there are no changes don't prompt. Any help would be greatly appreciated. By the way the program is written in VB.NET.
 
So, when the user makes a change, you set a boolean to True. Then in the Form Closing, ask if the user wants to save changes only if the boolean is true. :)
 
True

Yes this is true but how in the form do I determine if the user has made a change? The form as many textboxes and is using a tab control with each tab populated with different controls, what determininant factor to set the boolean equal to true?
 
Try using the TextBox.Validating Event event and either


make a control that derives from the TextBox Class and add a property such as 'TextChanged' that retuns a boolean if the text changed, the textbox will need to know what it's original text was so I would make it have only 1 constructor with 1 parameter, a string, that is supposed to be the original text. Doing this will probably be more efficient and you don't nee to use the Validating Event.


- or -

Using some type of hashtable that stores the texbox's id, and the original text of the textbox, listen to the Validating Event, and compare the text with what was in the hashtable, and you'll know if the text changed or not.
 
Back to Iceplug's post, make a Boolean variable like dataModified in your form's declaration section. When opening a new file or saving the current file set dataModified to False. Then in all the events that add, edit or in anyway modify the current data set dataModified to True.

Then in your form's closing event add
Visual Basic:
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If dataModified Then
            e.Cancel = True 'keep the form open
            'pop up a message box asking to save changes
        End If
    End Sub

From the message box's DialogResult:
If Yes then save the data and change dataModified to False and close
If No then change dataModified to False and close
If Cancel then do nothing

This is about the simplest way to do it.

'opps I re-read your post and I think that you already know this.....sorry.
 
Back
Top