We are developing a .NET application that uses a few custom controls.
One specific control is a simple “text editor” like control, where some content gets loaded into, based on the user’s selection.
In cases where the user modifies the text in this control, and then makes a different selection which would overwrite that content, we would like to have the usual popup saying “Do you want to save pending changes? YES/NO/CANCEL”.
I am considering which level of the application should be the one responsible for this: the control itself or the code that uses it?
I have been looking into the existing CancelEventArgs class, and considering using it, however i am not sure if this is a good fit for this specific scenario.
Example code i was thinking of:
When setting the text of the custom control, raise the “BeforeChanged” event. This will be handled and will allow to cancel the operation.
public void SetText(string text)
{
CancelEventArgs args = new CancelEventArgs();
// Raise the BeforeTextChanged event.
BeforeTextChanged(args);
// If the user cancelled the operation - do not modify text.
// For example, user code will check if needs saving, show the popup, etc.
if (args.Cancel)
{
return;
}
}
I don’t think you need to handle this with events – it will just complicate matters. This is linear problem and a dialog popup will halt the current thread and give you enough control to handle the user’s decision. The event in this scenario is the ‘UserSelectionChanged’ event. No need for more.