mark as unsaved if the user changes ANYTHING?
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
Yea, the most obvious solution is the tedious one: trap each input control's change event and set an
isDirty
type flag for the form to true. But... you could think of this as a more general-purpose problem too. It wouldn't be difficult to write a separate controller class - one that could take a form as input, loop through all of its controls, and assign event handlers for the input control change events. This controller would have anInitialize(Form frm)
method and anIsDirty
read/write property. You would then instantiate this controller in the Load event for each winForm you wish to track this way. When you save a document from the winForm, you set the controller'sIsDirty
flag to false. Then whenever the input changes on the form, the controller has already established event handlers that sets itsIsDirty
flag to true. Once the controller class is built, it becomes very simple to use in your winForm and reusable across multiple forms. -
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
For a system (similar to a Windows Service) I wrote a while back I had to write an editor for its configuration. The editor had to alert the running process of any saved changed. I didn't want false-positives (e.g. changing a value from 2 to 3 and back to 2), so as to avoid interupting the process needlessly. It wasn't much data so I had it save a copy of the data it read in. At exit it compared the two copies and only saved and alerted if there were actual changes. I realize this may not apply to your current situation, but maybe it'll help someone else someday.
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
Great suggestions you've had. This[^] is one way of implementing it that I've come across. It's old code, and some of what it uses is marked as obselete, but may be a starting point. It would be very useful if
System.Windows.Forms.Control
had anIsDirty
read only property and anIsDirtyChanced
event, then it would cascade through to every control we use, and would automatically be there in any new controls we create.Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
Here's a link to a blog post that fleshes out more what I was talking about: http://mishainthecloud.blogspot.com/2009/07/simple-dirty-tracking-for-winforms-in-c.html[^] I hope this is helpful.
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
Sounds good, but I think I have radio buttons, check boxes, numeric updowns and textboxes, I can only use the same handler if they all use normal EventArgs.
-
I'm writing an app where you can input a lot of data in a lot of different controls (textboxes, updowns, etc.) Now I want to implement a function you see in most other apps where you input information and save it: the moment the user changes a saved file or inputs something into a new file, the file is unsaved and upon closing it, the user will be prompted if he wants to save changes. The only way I can think of to achieve this is to use each input control's change event (change text for textbox, change value for UpDown, etc.) and set an "unsaved" bool to true. When the user wants to exit the app, check the bool and display a message box if necessary. Is there an easier way other than using all the events (I have a lot of data, so that would be about half the code of each form or more)?
I just wrote a post with sample code that I thought you might be interested in. It shows another approach to dirty tracking that avoids the false positives one gets with the "changed events" method: http://mishainthecloud.blogspot.com/2009/07/slightly-more-sophisticated-winforms.html[^] I hope it's helpful.