【问题标题】:c# Filedialog problemsc# 文件对话框问题
【发布时间】:2013-06-25 12:44:28
【问题描述】:

我对 c# 中的 opfiledialog 函数有疑问。当我不使用 openfiledialog 选择文件时,它会自动在我的文本框中输入一个文本。该文本将是“filedialog1”。我该怎么做才能解决这个问题。

using System;
using System.Windows.Forms;
using System.IO;

namespace Flashloader
{
    public partial class NewApplication : Form
    {

        private toepassinginifile _toepassinginifile;
        private controllerinifile _controllerinifile;



        //private controllerinifile _controlIniFile;

        public NewApplication(toepassinginifile iniFile)
        {
            _controllerinifile = new controllerinifile();
            _toepassinginifile = iniFile;

            InitializeComponent();
            controllerComboBox.DataSource = _controllerinifile.Controllers;
        }

        public bool Run()
        {
            var result = ShowDialog();
            return result == System.Windows.Forms.DialogResult.OK;            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";

            openFileDialog1.Title = ("Choose a file");
            openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Toepassing toepassing = new Toepassing();

            toepassing.Name = nameBox.Text;
            toepassing.Controller = (Flashloader.Controller)controllerComboBox.SelectedItem;
            toepassing.TabTip = descBox.Text;
            toepassing.Lastfile = openFileDialog1.FileName;
            fileBox.Text = openFileDialog1.FileName;

            if (nameBox.Text == "")
                MessageBox.Show("You haven't assigned a Name");
            else if (controllerComboBox.Text == "")
                MessageBox.Show("You haven't assigned a Controller");
            else if (descBox.Text == "")
                MessageBox.Show("You haven't assigned a Desciption");
            else if (fileBox.Text == "")
                MessageBox.Show("You haven't assigned a Applicationfile");
            _toepassinginifile.ToePassingen.Add(toepassing);
            _toepassinginifile.Save(toepassing);

            MessageBox.Show("Save Succesfull");

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var newcontroller = new Newcontroller(_controllerinifile);
            newcontroller.ShowDialog();
            controllerComboBox.DataSource = null;
            controllerComboBox.DataSource = _controllerinifile.Controllers;

        }
    }
}

感谢大家的帮助

【问题讨论】:

  • 你可以验证一下,if(filebox.text == "filedialogue1"){Messagebox.Show("You have not selected an item noob");}
  • 您需要修改您的问题以仅包含相关代码。您不应该期望人们通过您的代码查看相关部分
  • 当您不选择文件而只需按 button3 时,您会在分配的文本框中获得值 filedialog。
  • @PhilipGullick 直到有人真正尝试打开一个名为 filedialog1 的文件
  • @KooKiz,是的,但文件名通常也有扩展名。我从来没有想过将文件另存为 filedialog1... :)

标签: c# winforms openfiledialog


【解决方案1】:
   private void button3_Click(object sender, EventArgs e)
        {

        toepassing.Lastfile = openFileDialog1.FileName;// Dont do this
        fileBox.Text = openFileDialog1.FileName; //or this

我不清楚您为什么坚持打开文件对话框,我会亲自执行以下操作

using(OpenFileDialog ofd = new OpenFileDialog())
{
      if(ofd.ShowDialog() == DialogResult.OK)
     {
         classStringVariable = ofd.FileName;
         fileBox.Text = ofd.FileName;
     }
}

然后在按钮 3 中

toepassing.LastFile = classStringVariable ;
fileBox.Text = classStringVariable ;

【讨论】:

  • 但是他应该怎么做……?这并不能真正作为答案
【解决方案2】:

当您使用表单设计器在表单上添加 OpenFileDialog 控件时,设计器会在属性 FileName 处分配值 openFileDialog1
我想您已将某些内容设置为属性 FileName 的初始值。然后在 button_click3 中,您没有办法检查 DialogResult,因此您无条件地获得了这个默认值。

修复它从设计器 FileName 属性中删除此默认值

【讨论】:

    【解决方案3】:

    只需在显示对话框之前添加openFileDialog1.FileName= "";

    openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
    
    openFileDialog1.Title = ("Choose a file");
    openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
    openFileDialog1.RestoreDirectory = true;
    openFileDialog1.FileName = "";
    if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
    {
        fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
    }
    

    在您的button3_Click 事件中,无论如何您都在检查空字符串文件名,因此他们会收到正确的错误消息,并且在打开对话框时不会显示一些奇怪的任意默认名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 2017-05-25
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多