c#文件对话框问题

本文关键字:问题 对话框 文件 | 更新日期: 2023-09-27 18:19:45

我有一个关于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;
        }
    }
}

感谢所有人的帮助

c#文件对话框问题

   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 ;

使用窗体设计器在窗体上添加OpenFileDialog控件时,设计器会在属性FileName处分配值openFileDialog1
我想您已经为属性FileName设置了一些初始值。然后在button_click3中,您无法检查DialogResult,因此您可以无条件地返回此默认值。

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

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