1

I have a question about the opfiledialog function within c#. When i dont select a file with openfiledialog it put's a text automaticly in my textbox. That text will be "filedialog1". What can i do to fix this.

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; } } } 

Thanks all for the help

5
  • You could just validate it, if(filebox.text == "filedialogue1"){Messagebox.Show("You haven't selected an item noob");} Commented Jun 25, 2013 at 12:49
  • You need to modify your question to only include relevant code.. you shouldn't expect people to look through your code for the revant parts Commented Jun 25, 2013 at 12:49
  • 1
    When you don't select a file and just press button3 you get the value filedialog in the assigned textbox. Commented Jun 25, 2013 at 12:50
  • @PhilipGullick Until someone really tries to open a file called filedialog1 Commented Jun 25, 2013 at 12:53
  • @KooKiz, Yeah but the filename would usually have the extension too. It has never occurred to me to save a file as filedialog1... :) Commented Jun 25, 2013 at 12:54

3 Answers 3

1
 private void button3_Click(object sender, EventArgs e) { toepassing.Lastfile = openFileDialog1.FileName;// Dont do this fileBox.Text = openFileDialog1.FileName; //or this 

Its unclear to me why you are holding onto an Open file dialog I would personally do the following

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

Then in button 3

toepassing.LastFile = classStringVariable ; fileBox.Text = classStringVariable ; 
Sign up to request clarification or add additional context in comments.

1 Comment

but then what should he do...? This doesn't really qualify as an answer
0

When you use the Form Designer to add an OpenFileDialog control on you form, the designer assigns at the property FileName the value openFileDialog1.
I suppose you have set something as the initial value for the property FileName. Then in button_click3 you have no mean to check for the DialogResult and thus you get inconditionally this default back.

Fix it removing this default from the designer FileName property

Comments

0

Just add openFileDialog1.FileName= ""; before you show the dialog.

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)); } 

In your button3_Click event you're checking for an empty string file name anyways, so they would get the correct error message and they wouldn't have some weird arbitrary default name show up when they open the dialog.

Comments