Windows Develop Bookmark and Share   
 index > Windows Forms General > SaveFileDialog actions wrong
 

SaveFileDialog actions wrong

I have a strange issue. Seems like a few others have had it, but I couldn't find a post on msdn about it.

SaveFileDialog either returns DialogResult.OK or DialogResult.Cancel. The problem is if the file name the user wants tosave the file asalready exists in the current directory, another dialog pops up and asks if the user wants to overwrite. Even if I click "Yes", the SaveFileDialog returns DialogResult.Cancel. Even if this is the correct action C# creators intended to have happen, it goes against all logic and doesn't allow me to know that the user wants to continue on with the save.

On another site's forum, someone responded with the answer " turn SaveFileDialog.OverwritePrompt to false". That will not work for me. I want the user to be warned when they are about to overwrite an existing file, but if they choose to continue I want SaveFileDialog to return DialogResult.OK.

Anyone with a solution to this odd problem I would have never expected to see?

SaveFileDialog saveFileDlg = new SaveFileDialog();

saveFileDlg.Filter = "Text File (*.txt)|*.txt" ;

saveFileDlg.Title = "Export To Text File";

saveFileDlg.RestoreDirectory = false;

DialogResult dr = saveFileDlg.ShowDialog();

if (dr != DialogResult.OK)

return;

//Rest of what to do if user chose to save file

Brandon Williams  Friday, June 09, 2006 10:34 PM

I don't see the symptoms you do. I cut-and-pasted your first five lines of code into a button-click handler in a small test pogram and replaced your 'if' test with Console.Writeline (dr).

If I choose an existing file name I get the 'do you want to overwrite" warning but if I click "yes" then ShowFileDialog returns OK not cancel.

I tried it with 2.0 and 1.1 frameworks and get the same result

Obviously you are getting different results, possibly the difference is where we invoke ShowDialog. Although not directly applicable, there is a KB article about a known Visual Basic bug with your symptoms

BUG: SaveFileDialog object returns "Cancel" whether or not you overwrite an existing file http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B327213

Frank Boyne  Friday, June 09, 2006 11:28 PM

thanks for the reply. that article sparked a few more things for me to try. still no luck though. this is really wierd...and a really stupid bug to be getting.

HELP ME people! :-)

Brandon Williams  Saturday, June 10, 2006 12:32 AM

Was the code you posted exactly what you are running or were you paraphrasing? Obviously there's something different between our two attempts to reproduce the problem, the trick is figuring out what.

If you were paraphrasing, it might be worth creating a C# forms application, drop one button on the form and put exactly what you posted in the click handler just to be absolutely sure you get the same behaviour as my test case. If you do, we'll know the difference is likely something to do with how our respective systems are set up.

You could also try your same test program on another system if you have one handy?

Frank Boyne  Saturday, June 10, 2006 3:16 AM
that was the exact code. when I put the code in its own project it works as it should. so something has to be wrong with my other project. any ideas?
Brandon Williams  Monday, June 12, 2006 11:40 PM

Well there is one thing different in the test program vs. my actual program.... in my actual program the save dialog is being called from a worker thread, not the UI thread. I still see the dialog pop up ok, but does anyone think that not calling it from the main thread is making any sort of difference?

I meanI could set it up to have the worker thread use a delegate to have the main thread actually create the save dialog form. But if I do that, I don't know how to send the result of the save dialog box back to the worker thread to continue the work. Would I need to create a 2nd delegate, just backwards from what I did the first time?

Any idea's?

Brandon Williams  Wednesday, June 14, 2006 12:32 AM

Please post Windows Forms related questions to the Windows Forms forum. Reserve the C# forum for questions specifically related to C# itself. I'm moving the thread to the Windows Forms forum.

Thanks.
Michael Taylor - 6/13/06

TaylorMichaelL  Wednesday, June 14, 2006 2:50 AM

You can use google to search for other answers

Custom Search

More Threads

• VB.NET 2005 - Web Browser - New Window
• user Control inside the panel is not resizing
• DoDragDrop on TreeView Problem
• MS Excel ComboBox
• Window always on top but does cover other windows
• ActiveDocument
• how to convert
• Button Image Transparent Background
• AutoGenerateColumns
• Checkedlistbox and Listview...if an item is unchecked from the checkedlistbox I want the same item removed from the Listview..