Using SaveFileDialog In Windows.Forms

In this article, I am going to explain how to use a SaveFileDialog in a Windows.Forms app using Visual Studio 2017.
The SaveFileDialog prompts the users while saving files. This control allows the user to set a file name for a file. Then, you can use the event handling mechanism to add a custom code. This writes the file that the user wants to save. 
STEP 1 - Create a new project

Let's create a new project using Visual Studio 2017.

Select New Project->Visual C#->Windows Forms App (.NET Framework), give your project a name and click OK.
SaveFileDialog In Windows Forms 
This action creates a WinForms project with a default form and you should see the Windows Designer.

STEP 2 - Drag and Drop Controls

Let's add a SaveFileDialog control to the form by dragging it from the Toolbox and dropping it to the form. You will see that a SaveFileDialog is added to the form. This control is now available for you in the code behind.
SaveFileDialog In Windows Forms 
Additional controls
Now, let's add other controls, like Font Dialog, Color Dialog, Label, Button control to the form by dragging these from the toolbox. You may also want to change the properties of the other controls.
The Button control is used to open the SaveFileDialog. Like any Dialog, you must call the ShowDialog method to open your saveFileDialog. To add a click event handler to the saveFileDialog, double click on the button in the designer. 
Font and Color Dialog controls are used to change the font and color of the content displayed in the form.
STEP 3 - Coding for Button Click Event

You can add a button-click event handler by simply double-clicking on the button control event handler. We can save the file using saveFileDialog after changing the color and font using color and font Dialog control. 
  1. public partial class Form1 : Form  
  2.     {  
  3.         public Form1()  
  4.         {  
  5.             InitializeComponent();  
  6.         }  
  8.         private void button1_Click(object sender, EventArgs e)  
  9.         {  
  10.             DialogResult colorResult = colorDialog1.ShowDialog();  
  11.             if ( colorResult == DialogResult.OK)  
  12.             {  
  13.                 panel1.BackColor = colorDialog1.Color;  
  14.             }  
  15.         }  
  17.         private void button2_Click(object sender, EventArgs e)  
  18.         {  
  19.             DialogResult fontResult = fontDialog1.ShowDialog();  
  20.             {  
  21.                 if (fontResult == DialogResult.OK)  
  22.                 {  
  23.                     label1.Font = fontDialog1.Font;  
  24.                 }  
  25.             }  
  27.         }  
  29.         private void button3_Click(object sender, EventArgs e)  
  30.         {  
  31.             String font = fontDialog1.Font.Name;  
  32.             String color = colorDialog1.Color.ToString();  
  33.             DialogResult saveresult = saveFileDialog1.ShowDialog();  
  34.             if ( saveresult == DialogResult.OK)  
  35.             {  
  36.                 try  
  37.                 {  
  38.                     String path = saveFileDialog1.FileName;  
  39.                     StreamWriter sw = new StreamWriter(path);  
  40.                     sw.WriteLine(font);  
  41.                     sw.WriteLine(color);  
  42.                     sw.Close();  
  45.                 }  
  46.                 catch( Exception  error)  
  47.                 {  
  48.                     MessageBox.Show("oops");  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53. }  
STEP 4 - Compile and Run

Now, simply compile and run the application. Click the "Change color" button. Choose the color and click OK.
SaveFileDialog In Windows Forms 
After choosing the color, click the "Change font" button. Choose the font and click OK.
SaveFileDialog In Windows Forms 
Now, click the "Save" button. The file can be saved to the disk in a local folder. Once you click on the file in the local folder, the color and font type will be displayed in Notepad.
SaveFileDialog In Windows Forms 
SaveFileDialog In Windows Forms
In this basic article, you saw how to use a saveFileDialog along with other controls. Hope you found this article interesting. For any feedback, please post your comment at the bottom of this article. Thank you!.