1

我对 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;

        }
    }
}

谢谢大家的帮助

4

3 回答 3

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 ;
于 2013-06-25T12:50:03.060 回答
0

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

于 2013-06-25T13:09:45.380 回答
0

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

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

于 2013-06-25T12:57:37.027 回答