MessageBox在SaveFileDialog之后没有显示(聚焦)

时间:2013-05-31 13:29:45

标签: c# winforms visual-studio-2010

由于某些原因,在我的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");

        }
    }
}

4 个答案:

答案 0 :(得分:4)

我尝试了这个,它立即显示:

 MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");

答案 1 :(得分:2)

请尝试使用此消息框。

MessageBox.Show(this,"Probably removed the original file.");

答案 2 :(得分:1)

我创建了一个新项目并粘贴了您的代码,它对我有用。确保在运行之前完成了完全重建。另外,用这一行:

dialog.FileName = DateTime.Now.ToString(format) + "." + ext;

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

答案 3 :(得分:1)

也许您应该将SaveFileDialog置于使用中以确保在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");
        }
    }
}
相关问题