Introduction
In this article, I am going to explain how to use a SaveFileDialog in a Windows.Forms app using Visual Studio 2017.
SaveFileDialog
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.
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.
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.
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- DialogResult colorResult = colorDialog1.ShowDialog();
- if ( colorResult == DialogResult.OK)
- {
- panel1.BackColor = colorDialog1.Color;
- }
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- DialogResult fontResult = fontDialog1.ShowDialog();
- {
- if (fontResult == DialogResult.OK)
- {
- label1.Font = fontDialog1.Font;
- }
- }
-
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- String font = fontDialog1.Font.Name;
- String color = colorDialog1.Color.ToString();
- DialogResult saveresult = saveFileDialog1.ShowDialog();
- if ( saveresult == DialogResult.OK)
- {
- try
- {
- String path = saveFileDialog1.FileName;
- StreamWriter sw = new StreamWriter(path);
- sw.WriteLine(font);
- sw.WriteLine(color);
- sw.Close();
-
-
- }
- catch( Exception error)
- {
- MessageBox.Show("oops");
- }
- }
- }
- }
- }
STEP 4 - Compile and Run
Now, simply compile and run the application. Click the "Change color" button. Choose the color and click OK.
After choosing the color, click the "Change font" button. Choose the font and click OK.
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.
Summary
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!.