保存文件对话框后,MessageBox没有显示(焦点)。

8
由于某种原因,在我的SaveFileDialog之后,我的应用程序将永远不会显示MessageBox。我是否遗漏了什么?或者这是一个线程问题吗?
我使用VS 2010 Express作为Windows窗体应用程序运行。
我没有收到任何异常。
补充一下:当我逐步执行代码时,一切似乎都很顺利。这很奇怪,所以我认为这是一个时间问题。
正如LarsTech和其他人指出的那样,MessageBoxes确实会显示出来,但焦点消失了;换句话说,MessageBox被推到其他窗口后面或最小化了。这是一个问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
namespace SpeedDating
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {

        string filename = "test.test"; // args[0];
        string ext = filename.Substring(filename.LastIndexOf('.'));
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Title = "SpeedDating App";
        dialog.RestoreDirectory = true;
        dialog.CheckFileExists = false;
        dialog.CheckPathExists = false;
        dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext;

        DialogResult result = dialog.ShowDialog();
        if (result == DialogResult.OK && dialog.FileName != "")
        {
            try
            {
                FileStream outfs = File.Create(dialog.FileName);
                FileStream infs = File.Open(filename, FileMode.Open);
                infs.CopyTo(outfs);
                infs.Close();
                outfs.Close();
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show("Probably removed the original file.");
            }
        }
        else
        {
            MessageBox.Show("No path found to write to.");
        }

        MessageBox.Show("I came here and all I got was this louzy printline");

        }
    }
}

是的,我运行了这段代码,它可以正常工作。还有其他需要注意的事项吗? - Bearcat9425
如果没有其他异常,尝试添加catch(Exception ex),这应该就可以了。另外,如果我是你,我会在括号里添加if条件 -> if ((result == DialogResult.OK) && (dialog.FileName != "")),以确保一切都正确... - Ms. Nobody
@LarsTech:有可能是,那么可能不是一个很大的问题。 - RobotRock
3
这就是为什么MessageBox.Show()有接受window参数的方法。这确保了该框始终位于该窗口顶部。让MessageBox去寻找一个窗口可能在某些情况下不太可靠。例如,在没有窗口的情况下。最好避免将用户界面添加到没有界面的程序中。 - Hans Passant
@Cody Gray:是的,我知道。如果这让你放心,我已经为他点赞了。快速约会一个文件需要一个输入流和一个输出流,然后将一个写入另一个。哦,我真是太粗俗了。 - RobotRock
显示剩余8条评论
4个回答

5
我尝试了这个,它立即显示出来:
 MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");

1
这接近我最终所做的事情;使用上述设置创建一个表单,但只有一个表单来掌管它们。 - RobotRock

2
尝试使用以下内容来创建您的消息框。
MessageBox.Show(this,"Probably removed the original file.");

如果我创建一个使用Show()显示的表单,它就能正常工作。否则,它将无法显示MessageBox。但是,如果我创建一个新的表单并显示它,我将得到一个空的窗口,这不是我想要的。 - RobotRock
我刚刚进行了一次编辑,基本上你需要告诉消息框它的所有者是当前窗体,这样它就会将自己带到前面,请尝试使用此方法。而不是新表单。我之前发布了我的控制台消息。我忘记你说你正在运行一个WinForms应用程序。 - Bearcat9425
"this"会无效,因为它在静态的Main()方法内部。 - Edper
他说它正在作为Windows窗体应用程序运行,而不是控制台。如果它是WinForms应用程序,那么这是正确的。对于控制台应用程序,他需要像另一个答案发布的那样创建新表单,或者像我之前在考虑控制台时发布的答案一样。 - Bearcat9425

1
或许你应该将 SaveFileDialog 放在 using 语句中,以确保在 MessageBox 调用之前进行处理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
namespace SpeedDating
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {
            string filename = "test.test"; // args[0];
            string ext = filename.Substring(filename.LastIndexOf('.'));
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Title = "SpeedDating App by K.Toet";
                dialog.RestoreDirectory = true;
                dialog.CheckFileExists = false;
                dialog.CheckPathExists = false;
                dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext;

                DialogResult result = dialog.ShowDialog();
                if (result == DialogResult.OK && dialog.FileName != "")
                {
                    try
                    {
                        FileStream outfs = File.Create(dialog.FileName);
                        FileStream infs = File.Open(filename, FileMode.Open);
                        infs.CopyTo(outfs);
                        infs.Close();
                        outfs.Close();
                    }
                    catch (NotSupportedException ex)
                    {
                        MessageBox.Show("Probably removed the original file.");
                    }
                }
                else
                {
                    MessageBox.Show("No path found to write to.");
                }
            }

            MessageBox.Show("I came here and all I got was this louzy printline");
        }
    }
}

1
我创建了一个新项目并粘贴了你的代码,它对我有效。确保在运行之前进行了完整的重建。此外,在这一行中:
dialog.FileName = DateTime.Now.ToString(format) + "." + ext;

对话框将以文件名开头。因此,只有按下取消按钮(假设您没有首先清除保存对话框)才会触发消息框。无论哪种方式,我都通过未能通过您的IF测试来弹出了消息框。您的代码看起来没问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接