Before I can finish my rewrite of .NETpad's file operations class and fully implement Close, I had to write some other code first. The Close operation is difficult, and there are multiple checks to make whenever the user tries to close a tab or the entire app, thanks to the multiple documents and tabs the app now supports. And in working through this major task, I decided I needed a few new things:
A rewritten/updated SaveConfirmationDialog dialog class
A NeedsToBeSaved() method to test an individual tab/document to see whether it, well, needs to be saved
Naturally, I did this out of order. But that's because I knew I could fall back on the system MessageBox before working on SaveConfirmationDialog. This WPF dialog, among other things, supports up to three buttons. I need three--for "Save," "Don't save," and "Cancel"--and I wanted to rewrite my custom Confirmation dialog to work the same way. And more elegantly than was currently the case.
But first things first.
? NeedsToBeSaved()
My FileOperations class has a method called SaveOrSaveAs() that couldn't be simpler: It determines whether to call Save() or SaveAs() based on the state of the current document and then returns a Boolean value indicating whether the operation completed successfully.
What it doesn't do is determine whether the current document needs to be saved in the first place. That can be OK: Because it returns a value, I can (and do) use it in a conditional check, like so:
if (fo.SaveOrSaveAs(dts[TabIndex], TextBox1.Text))
{
UpdateTab(dts[TabIndex]);
}
But as I worked on improving the .NETpad 4.0 rewrite to include a confirmation dialog--the first implementation simply skipped that check so I could focus on the core tab/document management functionality--I realized I needed something more. And so I started working on a superset replacement that does everything. It's called NeedsToBeSaved(). And if I do this correctly, I can just replace SaveOrSaveAs() with this completely.
Tied to this, I wanted to rewrite my the custom dialog code that I use for my current Save confirmation dialog. Thinking ahead for a change, I decided to implement NeedsToBeSaved() using the system MessageBox instead of rewriting my Save confirmation dialog too. Once this worked properly, I could move on to the new dialog code, which would emulate how MessageBox works in a three button configure (Yes/No/Cancel, though in this case, it will be Save/Don't save/Cancel).
NeedsToBeSaved() is pretty simple too. I pass it the same parameters as SaveOrSaveAs()--a DocumentTab and text representing the current document--and it, too, returns a Boolean value indicating success. At a high level, it looks like so.
The trick, of course, is that switch statement.
Working backward through the choices...
Cancel. If the user cancels the operation, NeedsToBeSaved() returns a false value, indicating failure. (This way, the calling method can cancel whatever it's doing. For example, if...
With technology shaping our everyday lives, how could we not dig deeper?
Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.