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

フォーム デザイナーを使用して OpenFileDialog コントロールをフォームに追加すると、デザイナーはプロパティ FileName に value を割り当てますopenFileDialog1
プロパティ FileName の初期値として何かを設定したとします。次に、button_click3 で DialogResult をチェックする手段がないため、無条件にこのデフォルトに戻ります。

デザイナの FileName プロパティからこのデフォルトを削除する問題を修正

于 2013-06-25T12:57:37.027 に答える
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 に答える