Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用OpenFileDialog后,MessageBox不会';不要在顶部打开_C#_Visual Studio 2010 - Fatal编程技术网

C#使用OpenFileDialog后,MessageBox不会';不要在顶部打开

C#使用OpenFileDialog后,MessageBox不会';不要在顶部打开,c#,visual-studio-2010,C#,Visual Studio 2010,我们的主数据库应用程序有一个功能,可以为指定的工单导出Excel工作簿或Access mdb。然后将这些文件发送给我们的分包商,以填充所需的数据。我正在构建一个应用程序,该应用程序连接到文件,并在将数据导入主数据库之前显示数据以供查看。很简单,对吧?我的问题是:应用程序会打开一个OpenFile对话框,供用户选择将作为会话数据源的文件。那很好用。如果我在它之后打开一个MessageBox,该框将在任何其他打开的窗口后面打开。在那之后,他们的反应是正确的。我只希望使用MessageBox进行错误处

我们的主数据库应用程序有一个功能,可以为指定的工单导出Excel工作簿或Access mdb。然后将这些文件发送给我们的分包商,以填充所需的数据。我正在构建一个应用程序,该应用程序连接到文件,并在将数据导入主数据库之前显示数据以供查看。很简单,对吧?我的问题是:应用程序会打开一个OpenFile对话框,供用户选择将作为会话数据源的文件。那很好用。如果我在它之后打开一个MessageBox,该框将在任何其他打开的窗口后面打开。在那之后,他们的反应是正确的。我只希望使用MessageBox进行错误处理,但问题很复杂。有人遇到过这个问题吗

代码中的消息框仅用于验证路径是否正确并解决此问题;但是,以下是我的代码:

    private void SubContractedData_Load(object sender, EventArgs e)
    {
        string FilePath;
        string ext;
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Microsoft Access Databases |*.mdb|Excel Workbooks|*.xls";
        ofd.Title = "Select the data source";
        ofd.InitialDirectory = ElementConfig.TransferOutPath();

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            FilePath = ofd.FileName.ToString();
            ext = FilePath.Substring((FilePath.Length - 3));
            if (ext == "xls")
            {
                MessageBox.Show(FilePath);
                AccessImport(FilePath);
            }
            else if (ext == "mdb")
            {
                MessageBox.Show(FilePath);
                AccessImport(FilePath);
            }
        }
        else
        {
            System.Windows.Forms.Application.Exit();
        }
    }

虽然不建议使用MessageBox来调试代码,但我认为最直接的问题是您正在表单的加载事件中进行调试

试着这样做:

protected override void OnShown(EventArgs e) {
  base.OnShown(e);

  // your code
}

您的问题肯定是您在加载
表单时试图这样做,因为表单尚未显示

另一种选择是将此功能移出Load事件,并给用户一个按钮来按下或类似的操作

换言之:

private void SubContractedData_Load(object sender, EventArgs e)
{
    // unless you're doing something else, you could remove this method
}
添加处理此功能的按钮:

private void SelectDataSourceClick(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Microsoft Access Databases |*.*|Excel Workbooks|*.xls";
    ofd.Title = "Select the data source";
    ofd.InitialDirectory = ElementConfig.TransferOutPath();

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        var FilePath = ofd.FileName.ToString();
        var ext = Path.GetExtension(FilePath).ToLower();

        switch (ext)
        {
            case ".xls":
                MessageBox.Show(FilePath);
                AccessImport(FilePath);
                break;
            case ".mdb":
                MessageBox.Show(FilePath);
                AccessImport(FilePath);
                break;
            default:
                MessageBox.Show("Extension not recognized " + ext);
                break;
        }
    }
    else
    {
        System.Windows.Forms.Application.Exit();
    }
}

MessageBox是糟糕的调试工具。另外,您可以使用
Path.GetExtension(…)
函数代替。谢谢,我不知道Path.GetExtension()。这是非常有用的。我仍然想知道为什么MessageBox会出现在后台。这非常有效。非常感谢。现在,出于我自己的启发,为什么在表单加载事件期间运行它会出现问题?@DanRicketts表单还没有出现在屏幕上,所以windows会感到困惑。这个问题在Windows 8上没有发生,但在我检查过的旧Windows 2003计算机上发生了。Ok。这是有道理的。谢谢你的帮助。谢谢你,我感谢你的帮助。实际上,我在菜单上添加了一个按钮,供用户点击。在查看了您提供的代码之后,我还添加了一个switch语句。起初我并不是因为只有两种文件类型,但我认为这样看起来更干净。我应该注意到,如果有人正在阅读本文,.GetExtension()将返回“.xls”而不是“xls”,并使用ext.Lower()确保大小写相同。有时候不是。