Show message via 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 a message box through the saveialog window after clicking Cancel in the SaveDialog field, that is, the Save dialog box must be present in the background.

0


source to share


7 replies


If the reason you want the "Cancel Save File Dialog" message box is because you close everything with unsaved changes, I suggest placing a call to the "Save File" dialog in a loop that continues until the flag is set. to stop the loop and bring up a message box if you don't get OK as a 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 save file dialog behaves consistently with how it works in other Windows applications, and you can let the user save the file (s) again if they accidentally hit "Cancel" in the Save File dialog box.

+4


source


You cannot do this with a class SaveFileDialog

.



+1


source


I'll have the second lubos. Unable to execute SaveFileDialog class.

What you basically want to do is capture the specific button click event on the SaveFileDialog, an event that the class doesn't provide you with. The solution, if you really want this functionality, is to flip your own save dialog so that you can handle each button however you want.

+1


source


As far as I know, you cannot accomplish what you want in pure .Net using SaveFileDialog. You can probably execute it if you log out to Windows and listen to the actual messages on Windows and respond to a click event message etc. I prefer to avoid this.

You can search for a third party dialog class or write your own.

+1


source


It is generally not a good idea to create a program whose user interface for interacting with the file system does not work the way most other Windows programs do. This is why there is no easy way to do it.

0


source


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

      

0


source


By the way, there is a more efficient way to display and check the dialog box. For example:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

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

      

0


source







All Articles