tags:

views:

3435

answers:

7
SaveFileDialog savefileDialog1 = new SaveFileDialog();
DialogResult result  = savefileDialog1.ShowDialog();
switch(result == DialogResult.OK)
    case true:
        //do something
    case false:
        MessageBox.Show("are you sure?","",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

How to show the messagebox over the savedialog box after clicking "Cancel" on the SaveDialog box i.e. the Save Dialog box should be present on the background.

+1  A: 

You can't do that with SaveFileDialog class.

lubos hasko
+1  A: 

I'll have to second lubos. Can't be done with the SaveFileDialog class.

What you basically want to do is capture a specific button click event on the SaveFileDialog, an event that the class does not make available to you. A solution if you really want this kind of functionality would be to roll your own save dialog so you can handle each button click your own way.

Lenny
+1  A: 

To my knowledge you cannot accomplish what you want in pure .Net using the SaveFileDialog. You can probably accomplish it if you go out to Windows and listen for the actual windows messages and respond to the click event message, etc. I prefer to avoid doing this.

You might look for a 3rd party dialog class, or write your own.

Jason Jackson
A: 

It's generally not a good idea to make a program whose user interface for interoperating with the file system doesn't work the same way most other Windows programs do. Which is why there's no easy way of doing this.

Robert Rossney
A: 

You can do it with some modification:

    private void Form1_Load(object sender, EventArgs e)
    {
        DialogResult result = showDialog();
        if (result == DialogResult.OK)
        {
            //Ok
        }
        else
        {
            DialogResult r = MessageBox.Show("Are you sure?", "Sure?", MessageBoxButtons.YesNo);
            if(r.ToString()=="No")
            {
                showDialog();
            }
        }
    }

    public DialogResult showDialog()
    {
        SaveFileDialog savefileDialog1 = new SaveFileDialog();
        DialogResult result = savefileDialog1.ShowDialog();
        return result;
    }
Daok
+2  A: 

If the reason for needing the message box on Cancel of the File Save dialogue is because you're shutting things down with unsaved changes, then I suggest putting the call to the File Save dialogue in a loop that keeps going until a flag is set to stop the loop and call the message box if you don't get OK as the result. For example:

// lead-up code

SaveFileDialog sft = new SaveFileDialog();
BOOL bDone;
do
{
  if (DialogResult.OK == sft.ShowDialog())
    bDone = true;
  else
  {
    DialogResult result = MessageBox.Show("Are you sure you don't want to save the changed file?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    bDone = (result == Yes) ? true : false;
  }
} while (!bDone);

// carry on

This way, the File Save dialogue behaves consistently with the way it does in other in Windows apps, and you get to let the user have another go at saving the file(s) if he accidentally hits Cancel in the File Save dialogue.

RobH
Thanks for a nice and simple answer.
Learner
A: 

By the way, there is a more efficient way of displaying and checking the Dialog. Like so:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

if( saveFileDialog1.ShowDialog() == DialogResult.OK )
{
   // Code here...
} else Application.DoEvents();
baeltazor