Skip to content

How do I detect changes in a form and promt user to save before closing?

Henry Vuong

Asked Sep 17, 2008, 5:59 PM EDT2 answers
Share
LinkedInX
Let's say I have a form, on this form there's a datagridview and some text boxes, all databound. If a user opens the form and make some changes, when he closes the form without clicking the "Save" button, a message box should pop up and promt the user to save the data.

I know how to call the message box when user closes the form, but I only want this box pops up when there's some unsaved data on the form. How do I detect the changes from the last save?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The easiest way I see to do that is to handle the DataGridView events such as RowsRemoved, CellEndEdit, etc... Itd be easier if there was something like a DataGridView.DataChanged event but we gotta work with what were given sometimes. Also any of the text boxes TextBox.TextChanged events and if any of those events are triggered, then set a boolean variable to such as changesMade = true; and evaluate that when the form closes. If its true, prompt the user to save, its its not, then dont sweat it :)
    0
  • 0
    I used DataGridView.CellValueChanged and TextBox.TextChanged, it works. Thanks
    0