How can I validate a filename prior to the SaveFileDialog closing?
-
21-08-2019 - |
Question
I am using a SaveFileDialog and would like to evaluate whether a file meets certain conditions before allowing it to be saved. If it doesn't meet the criteria, I don't want the SaveFileDialog to close when "Save" is clicked. I thought the FileOK might work, but the dialog looks like it is already closed by the time that event is fired, and I don't see a way to prevent it from closing in any case.
Solution
FileOK
is a CancelEventHandler
- you just have to set the Cancel
property of the CancelEventArgs
to true
.
OTHER TIPS
Try this approach from FileOK handler
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
//your conditions...
if (!openFileDialog1.FileName.Equals( "C:\\hello.txt" ) )
{
//if fail, set e.cancel
MessageBox.Show(@"File name must equal c:\hello.txt.");
e.Cancel = true;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow