OpenFileDialog and SaveFileDialog control in VB.NET

In this article you will learn about OpenFileDialog and SaveFileDialog and its Common properties with an example.
  • 6872
 

Open File Dialog:

A visual basic Open File Dialog control allow you to select a file to be opened. Open File Dialog's are Provided by Open FileDialog class. If you want to make visible any DialogBox at Runtime you will use the ShowDialog method of the DialogBox. If you are using OpenFileDialog control you have to set some properties like DefaultExt to set the default extension of the file, InitialDirectory to any drive(C,D,E) which should open when you use this control and RestoreDirectory to true. 
SaveFileDialog: 
A visual basic SaveFileDialog control is used to save a file in a specified location. This control is supported by the SaveFileDialog class. The properties of SaveFileDialog is same as the OpenFileDialog except the one notable property that is OverWritePrompt which shows a warning if you select to a name that is already exist. 
Properties of OpenFileDialog and SaveFileDialog control:-

  • DefaultExt:- This property allow you to set the default file extension. 
  • InitialDirectory:- By this property you will set the InitialDirectory which should open when you use the OpenFileDialog and SaveFileDialog. 
  • RestoreDirectory:- If this property set to true it will restores the original directory before closing. 
  • FileNames:- This will gives you the file names of all selected files.

How to use OpenFileDialog and SaveFileDialog control:
  • open a new Project.
  • Drag a MenuStrip, OpenFileDialog, SaveFileDialog control on form and set the OpenFileDialog's and SaveFileDialog's property DefaultExt to txt InitialDirectory to c: and RestoreDirectory to true. The form will look like below.


    OpenDialog-and-SaveDialog1.gif
    OpenDialog-and-SaveDialog1.1.gif
    OpenDialog-and-SaveDialog2.gif
     
  • Write the below code

       Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e AsSystem.EventArgs
        
    Handles SaveToolStripMenuItem.Click
            SaveFD.ShowDialog()
        End Sub
        Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e AsSystem.EventArgs)
        Handles OpenToolStripMenuItem.Click
            OpenFD.ShowDialog()
        End 
    Sub

    Output:-  
    OpenDialog-and-SaveDialog3.gif


    OpenDialog-and-SaveDialog4.gif
    OpenDialog-and-SaveDialog5.gif

    OpenDialog-and-SaveDialog6.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.