【问题标题】:OpenFileDialog C# Asking for opening twice?OpenFileDialog C# 要求打开两次?
【发布时间】:2015-05-04 11:54:21
【问题描述】:

我使用 Telerik Browse Editor 在我的程序中打开了一个文件。但由于某种原因,它两次要求我提供文件。从下面的代码中谁能看出原因?

   private void radBrowseEditor1_ValueChanged(object sender, EventArgs e)
   {
        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.Title = @"Open .HRM File";
            openFileDialog.InitialDirectory = @"C:\Users\mike\Desktop";
            openFileDialog.Filter = @"HRM files (*.hrm)|*.hrm|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {

            }

            using (StreamReader reader = new StreamReader(openFileDialog.FileName, System.Text.Encoding.Default))
            {
                HRM.Active.Raw = reader.ReadToEnd();
            }
        }
   }

可能真的很简单。很确定我在安装 Telerik 之前有一个工具条时使用的代码,它运行良好。

【问题讨论】:

  • 使用调试器,添加断点。 ShowDialog() 是否被调用了两次?
  • 也许你打电话给radBrowseEditor1_ValueChanged 两次。或将其分配给事件处理程序两次。
  • 回复这两个,不,我找不到他们中的任何一个被调用了两次。
  • radBrowseEditor1 是单选按钮吗?如果是这样,您是否只想在选中单选按钮时显示对话框?如果是这样,那么这个事件的开始应该是检查那个。
  • 浏览编辑器就像去文件和打开一样。

标签: c# telerik openfiledialog


【解决方案1】:

读完这个http://www.telerik.com/help/winforms/editors-browse-editor-working-with.html,看来你甚至不需要为openFileDialog操心了。看起来您应该检查该值是否为空。

private void radBrowseEditor1_ValueChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(radBrowseEditor1.Value.ToString()))
    {
        using (StreamReader reader = new StreamReader(radBrowseEditor1.Value.ToString(), System.Text.Encoding.Default))
        {
            HRM.Active.Raw = reader.ReadToEnd();
        }
    }
}

【讨论】:

  • 由于我的其余代码以及它从加载的文件中计算数据的方式,这不起作用。
  • @user2913240 除了对话窗口打开两次之外,您的原始解决方案是如何工作的?
  • 这是顶部的代码,这就是解决方案的工作原理。然后它从 txt 文件中逐行读取数据并将其放入 datagridview 中。我之前用于工具条菜单项_单击的相同代码,它工作得很好。
  • @user2913240 是否为 OpenFileDialog 设置了 radBrowseEditor1 的 DialogType 属性?如果是这样,我绝对不明白在 ValueChanged 事件中有 OpenFileDialog 代码和没有它之间的区别。一旦分配了 reader.ReadToEnd(),HRM.Active.Raw 会做什么?
  • 是的,我意识到发生了什么,不需要 OpenFileDialog 的代码!因为 DialogType 设置为 OpenFile Dialog。但我只是想知道在删除文件对话框代码时如何实现相同的流读取器。
猜你喜欢
  • 1970-01-01
  • 2018-11-04
  • 2010-11-21
  • 2011-07-18
  • 2012-09-21
  • 2022-08-10
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
相关资源
最近更新 更多