-9

I am using Forms in Visual Studio.I would like to display an error message when save is clicked on the save file dialog without giving the filename. How do I do it?

I've tried the following code but it did not work: I've tried 2 logics. 1)

if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{ 
    MessageBox.Show("Enter the Filename");
}   

2) This is the 2nd Logic

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) 
{ 
  if (saveFileDialog1_FileOk == saveFileDialog1.FileName) 
  { 
    MessageBox.Show("Enter the Filename"); 
  } 
  else 
  { 
    string name = saveFileDialog1.FileName; 
    string testvar = textBox1.Text; 
    File.WriteAllText(name, testvar); 
  } 
}

I want to display an error message when save button is clicked without entering anything in the File Name. I hope the question is clear!

Amr
  • 1
  • 3
  • 7
    What do you mean it *does not work*? Does VS give you an exception? Does your PSU blow up? Does your monitor turn into a raging ball of flames? Does your app go unresponsive? – It'sNotALie. Aug 19 '13 at 10:39
  • 2
    No, My keyboard started running away from my place!! – Amr Aug 19 '13 at 10:43
  • Your code looks like it should work. Can you post the entire `saveButton_Click` method? – Nolonar Aug 19 '13 at 10:51
  • private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { if (saveFileDialog1_FileOk == saveFileDialog1.FileName) { MessageBox.Show("Enter the Filename"); } else { string name = saveFileDialog1.FileName; string testvar = textBox1.Text; File.WriteAllText(name, testvar); } } – Amr Aug 19 '13 at 10:53
  • 2
    Don't put log code statements in a comment, it is unreadable. Instead edit your own question and update it with that code above. – Steve Aug 19 '13 at 11:02
  • By the way i would recommed 'string.IsNullOrWhiteSpace', this would check if a string is empty, null or if it fully consists of whitespaces (which is not equal to null or empty) – BudBrot Aug 19 '13 at 11:02
  • @user2696048: I edited your question and added the code you posted in the comment. Then I noticed that the new code has not the same `if(..)` condition than you original question code. What is it, have you tried both? – awe Aug 19 '13 at 11:12
  • Can you edit your question, and give us exactly the code you have tried, so that we can have better understanding of the the real problem. – awe Aug 19 '13 at 11:17

1 Answers1

3

SaveDialog doesn't really work like this - it won't return an empty string using the OK button, so you really need to check the DialogResult rather than the text of the string. Perhaps something like:

DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
    if (System.IO.File.Exists(saveFileDialog1.FileName))
    {
        //overwrite existing file here
    }
    else
    {
        //save as new file here
    }
}
else
{
    //dialog did not return from an OK button (e.g. cancel)
}

Also consider reading this answer which handles whether the file already exists in a different way.

Community
  • 1
  • 1
n4m16
  • 121
  • 8
  • This would just tell us whether the file exists or not. Here the dialog result would only check when Ok is pressed. This does not speak when the filename is empty. I hope you got the point. – Amr Aug 27 '13 at 06:50
  • The point I was making is that to click 'Save' in a save dialog you _can't have_ an _empty_ filename box - so a check for empty string is redundant. That is why you need to check for DialogResult.OK - which automatically means saveFileDialog1.FileName is NOT null or empty. The File.Exists check was just for completeness in the code. – n4m16 Sep 02 '13 at 11:41