【问题标题】:MessageBox not showing (focused) after SaveFileDialog在 SaveFileDialog 之后 MessageBox 不显示(聚焦)
【发布时间】:2013-05-31 13:29:45
【问题描述】:

出于某种原因,在我的 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 发现。 MessageBoxes 显示在 Visual Studio 编辑器的后面。
  • 我运行双屏,所以我的编辑器在一个屏幕上,应用程序在另一个屏幕上运行,所以它们对我来说工作得很好,但是是的,我可以在单个屏幕上看到这种情况。 @LarsTech 找到你的问题了吗?
  • 这就是为什么 MessageBox.Show() 具有接受 window 参数的重载。这确保了该框始终位于该窗口的顶部。在某些情况下,让 MessageBox 来查找窗口可能是不确定的。就像这个你没有一个窗口的地方。最好避免将用户界面固定在没有用户界面的程序上。

标签: c# winforms visual-studio-2010


【解决方案1】:

我试过这个,它马上就显示出来了:

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

【讨论】:

  • 这与我最终所做的很接近;创建一个具有上述设置的表单,但是一个表单来统治它们。
【解决方案2】:

为您的消息框试试这个。

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

【讨论】:

  • 如果我创建一个用 Show() 显示的表单,它就可以工作。否则它不会显示 MessageBox。但是,如果我创建一个新表单并显示它,我将有一个空的表单窗口,这是我不想要的。
  • 我刚刚做了一个编辑,基本上你想告诉消息框它的所有者是你的当前表单,所以它会自动出现在前面,试试这个。而不是新形式。我正在发布我的控制台混乱。我忘了你说你是作为一个 winforms 应用程序运行的。
  • "this" 无效,因为它在静态 Main() 中。
  • 他说它作为 Windows 窗体应用程序而不是控制台运行。如果它是一个 winforms 应用程序,这将是正确的。对于控制台应用程序,他需要创建新表单作为发布的另一个答案,或者在我考虑控制台之前如何发布我的答案。
【解决方案3】:

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

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

对话框将以文件名开头。因此,只有点击取消按钮(假设您没有先清除保存对话框)才会触发消息框。无论哪种方式,无论哪种方式,如果您的 IF 测试失败,我都会弹出消息框。您的代码看起来不错。

【讨论】:

    【解决方案4】:

    也许您应该将 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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多