简体   繁体   English

通过C#中的“保存”对话框显示消息框

[英]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 ie the Save Dialog box should be present on the background. 单击“保存”对话框上的“取消”后如何在“保存的日志”框上显示消息框,即“保存”对话框应显示在背景上。

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. 这样,“文件保存”对话框的行为与Windows应用程序中其他对话框的行为一致, 并且如果用户不小心在“文件保存”对话框中单击“取消”,则可以让用户继续保存文件。

您不能使用SaveFileDialog类来做到这一点。

I'll have to second lubos. 我将不得不第二次润滑。 Can't be done with the SaveFileDialog class. 使用SaveFileDialog类无法完成。

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. 您基本上想要做的是捕获SaveFileDialog上的特定按钮单击事件,该事件对该类不可用。 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. 如果您真的想要这种功能,一个解决方案是滚动您自己的保存对话框,以便您可以按自己的方式处理每个按钮的单击。

To my knowledge you cannot accomplish what you want in pure .Net using the SaveFileDialog. 据我所知,您无法使用SaveFileDialog在纯.Net中完成所需的操作。 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. 如果您去Windows并收听实际的Windows消息并响应click事件消息等,则可能可以完成此操作。我宁愿避免这样做。

You might look for a 3rd party dialog class, or write your own. 您可能会寻找第3方对话类,或者编写自己的对话类。

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

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. 通常,使程序的用户界面与文件系统互操作的方式与大多数其他Windows程序不同,通常不是一个好主意。 Which is why there's no easy way of doing this. 这就是为什么没有简单的方法可以做到这一点的原因。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM