views:

255

answers:

3

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.

A: 

.NET Controls: The Save File Dialog Box

DaDa
+3  A: 

FileOK is a CancelEventHandler - you just have to set the Cancel property of the CancelEventArgs to true.

Daniel Brückner
Thanks, that was exactly what I needed. BTW, you have a typo: "CencelEventArgs".
Andy Stampor
+2  A: 

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;

    }            
}
Steve