SaveFileDialog Box Component in VB.NET

The Windows Forms SaveFileDialog component is a pre-configured dialog box.
  • 2492
The Windows Forms SaveFileDialog component is a pre-configured dialog box. It is the same as the standard Save File dialog box used by Windows. It inherits from the CommonDialog class.

Use it as a simple solution for enabling users to save files in lieu of configuring your own dialog box. By relying on standard Windows dialog boxes, the basic functionality of applications you create is immediately familiar to users. Be aware, however, that when using the SaveFileDialog component, you must write your own file-saving logic.

You can use the ShowDialog method to display the dialog box at run time. You can open a file in read-write mode using the OpenFile method.

When it is added to a form, the SaveFileDialog component appears in the tray at the bottom of the Windows Forms Designer.
Saving Files Using the SaveFileDialog Component
The SaveFileDialog component allows users to browse the file system and select files to be saved. The dialog box returns the path and name of the file the user has selected in the dialog box. However, you must write the code to actually write the files to disk.

To save a file using the SaveFileDialog component

  • Display the Save File dialog box and call a method to save the file selected by the user.

    Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.

    The example below uses the DialogResult property to get the name of the file, and the OpenFile method to save the file. The OpenFile method gives you a stream to write the file to.

    In the example below, there is a Button control with an image assigned to it. When you click the button, a SaveFileDialog component is instantiated with a filter that allows files of type .gif, .jpeg, and .bmp. If a file of this type is selected in the Save File dialog box, the button's image is saved.

    Security Note   To get or set the FileName property, your assembly requires a privilege level granted by the System.Security.Permissions.FileIOPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.

    The example assumes your form has a Button control with its Image property set to a file of type .gif, .jpeg, or .bmp.

    Note   The FileDialog class's FilterIndex property (which, due to inheritance, is part of the SaveFileDialog class) uses a one-based index. This is important if you are writing code to save data in a specific format (for example, saving a file in plain text versus binary format). This property is featured in the example below.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Displays a SaveFileDialog so the user can save the Image
        ' assigned to Button2.

        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
        saveFileDialog1.Title = "Save an Image File"

        ' If the file name is not an empty string open it for saving.
        If saveFileDialog1.FileName <> "" Then
            ' Saves the Image via a FileStream created by the OpenFile method.
            Dim fs As System.IO.FileStream = CType _
               (saveFileDialog1.OpenFile(), System.IO.FileStream)
            ' Saves the Image in the appropriate ImageFormat based upon the
            ' file type selected in the dialog box.
            ' NOTE that the FilterIndex property is one-based.
            Select Case saveFileDialog1.FilterIndex
                Case 1
                    Me.Button1.Image.Save(fs, _
                Case 2
                    Me.Button1.Image.Save(fs, _

                Case 3
                    Me.Button1.Image.Save(fs, _
            End Select

        End If
    End Sub


When you will click on the Button1 the result will be :


Source Code :

Source code attach in the top of the article.


More Articles

© 2020 DotNetHeaven. All rights reserved.