Windows common dialogs - SaveFileDialog
Explains how to integrate the windows common task "save file dialog box" in our application.
The SaveFileDialog is used to display the Save As dialog box. The user can save a file by providing a specific name to the file.
1. Drag the SaveFileDialog control from the Dialogs section of the Toolbox and drop it on the form. The control will be added to the control tray. The default name for the control is saveFileDialog1.
2. Select the control by clicking on it in the control tray and set the following properties in the properties window:
|
Property |
Value |
|
FileName |
*.txt |
|
Filter |
Text File|.txt |
|
Title |
Save File As |
3. Switch to the code view and add a method - ShowSaveAs() as shown here:
void ShowSaveAs() { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { myFile = saveFileDialog1.FileName; FileSave(); } }
The ShowDialog() method of sveFileDialog1 will display the save as dialog box to the user. If the user specifies a file name and clicks the Save button, the file name will be assigned to the myFile variable using the FileName property of saveFileDialog1 (saveFileDialog1.FileName). We are then calling another method FileSave() that will do the actual saving:
void FileSave() { try { File.WriteAllText(myFile, txtInput.Text); } catch (IOException ex) { MessageBox.Show(ex.Message); } }
Inside the try block, we are calling the WriteAllText() method of the File class. This method takes two arguments, the file to be stored (specified by the myFile variable in the ShowSaveAs() method as shown earlier) and the source text (specified by txtInput.Text). The method writes the content of the textbox into the file. In case of exception, the catch block will display a message using the object of the IOException class.
Finally, we will call the ShowSaveAs() method from the click event of the Save As menu option:
private void mnuFileSaveAs_Click(object sender, EventArgs e) { ShowSaveAs(); }
When the user clicks the Save As menu option, the ShowSaveAs() method will be called which will display the save as dialog box. The user will provide a file name and then click the save button. Now, the actual saving operation will be performed by calling the FileSave() method from the ShowSaveAs() method.
Why have we performed the operation using two methods? Because we need to perform the simple save operation as well. The Save menu option, when clicked, will decide if the current file is a new file or an existing file. If the file is being saved for the first time, we need to display the save as dialog box. However, if an existing file is being saved after some changes, the application should save the existing file without displaying the save as dialog box.
4. Add the following code to the click events of the Save menu option and related toolbar button:
private void mnuFileSave_Click(object sender, EventArgs e) { if (myFile == "newfile") { ShowSaveAs(); } else { FileSave(); } }
Remember that early in our code, we have assigned the string "newfile" to the myFile variable. When the user opens or saves a file, the name of the actual file is stored in the myFile variable. But if the file is not yet saved (being created for the first time), the variable myFile will store its initial value (the string "newfile"). In the click event of the Save option, we are checking if the myFile has the value "newfile". If the condition is true, it indicates that the file has not been saved earlier. We will call the ShowSaveAs() method to display the save as dialog box. User will provide a name for the file and the ShowSaveAs() method in turn will call the FileSave() method to save the file with a given name.
However, if the user is working with an existing saved file, the myFile variable will contain the name of that file. In this case, the `if' condition will become false. The else part will execute in which, we are calling the FileSave() method directly without calling the ShowSaveAs() method. So the file will be saved without displaying the save as dialog box.