Learner
Learner

Reputation: 990

Show messagebox over Save dialog in C#

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.

Upvotes: 0

Views: 10802

Answers (7)

jay_t55
jay_t55

Reputation: 11652

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();

Upvotes: 0

RobH
RobH

Reputation: 1639

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.

Upvotes: 4

Patrick Desjardins
Patrick Desjardins

Reputation: 140843

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;
    }

Upvotes: 0

Robert Rossney
Robert Rossney

Reputation: 96750

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.

Upvotes: 0

Jason Jackson
Jason Jackson

Reputation: 17260

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.

Upvotes: 1

Lenny Sirivong
Lenny Sirivong

Reputation: 1031

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.

Upvotes: 1

lubos hasko
lubos hasko

Reputation: 25052

You can't do that with SaveFileDialog class.

Upvotes: 1

Related Questions