消息框不显示(聚焦)后保存文件对话框
本文关键字:保存文件 对话框 聚焦 显示 消息 | 更新日期: 2023-09-27 18:16:11
由于某些原因,在我的SaveFileDialog之后,我的应用程序将永远不会显示MessageBox。我是不是漏掉了什么?或者这是线程问题?
我使用vs2010 Express将应用程序作为Windows窗体应用程序运行。
我没有得到任何异常。
补充:当我逐步完成代码时,一切似乎都很顺利。这很奇怪,所以我认为这是一个时间问题。
LarsTech和其他人指出,MessageBoxes确实出现了,但是焦点不见了;换句话说,消息框被推到其他窗口后面或最小化。这是个问题。
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");
}
}
}
我试了这个,它立即显示:
MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");
在您的消息框中尝试此操作。
MessageBox.Show(this,"Probably removed the original file.");
我创建了一个新项目并粘贴了您的代码,它为我工作。请确保在运行之前完成了完整的重新构建。同样,用这一行:
dialog.FileName = DateTime.Now.ToString(format) + "." + ext;
对话框将以文件名开始。因此,只有点击取消按钮(假设您没有首先清除保存对话框)才会触发消息框。无论哪种方式,我得到的消息框弹出失败你的测试无论哪种方式。你的代码看起来不错
也许您应该将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");
}
}
}