The Save and Open File Dialogs are easy to implement and use. The hardest part is setting up the filter the way you want it to be. Even that will be considered easy after you see the format that’s used.
First of all, this is just a small article and not a complete overview of the Dialog objects. It is simply to help you get started. I will go over the most used features of the OpenFileDialog and SaveFileDialog objects. The code/comments in this article will work in Visual Basic.NET, Visual Basic 2008, VB 2010, and higher.
Your IDE toolbox should by default contain the OpenFileDialog and SaveFileDialog controls. You could just double click those to add them to your project.
But I prefer to create and initialize mine through code like below…
Dim OpenDLG As New OpenFileDialog Dim SaveDLG As New SaveFileDialog
The first feature I will mention is the .AddExtension property. That simply means that you can tell the dialog whether or not to add a extension if the user omits it. In other words, if your filter was setup for Jpeg files, and the user only put the filename without the .jpg, the dialog would automatically add the .jpg extension to it. I normally set this to true.
The next items are CheckFileExists and CheckPathExists. They are self explanatory and not really anything to say. It will simply check if the path or filename exists. I just leave these at default.
The next item I sometimes use, is .DefaultExtension. IF no extension is supplied, then the dialog will use the extension you supplied in this DefaultExtension property.
Filename is the next highly used feature and it is exactly what it says. This is the path and filename that the user selected.
Text1.Text = OpenDLG.FileName
That code will put the full path and filename the user selected in the textbox control.
The .Filenames() feature is used if you set the Dialog to allow the user to select Multiple files at a time.
The next common feature is the Filter property. This is where you tell the dialog which file types to show in the selection window. If you only want the user to see JPeg files then you would use this feature to show files with the .jpg extension.
So if you only wanted the user to see JPeg files with the extension “jpg”, you would setup the filter as below…
OpenDLG.Filter = "JPEG Files (*.jpg)|*.jpg"
When the dialog is shown, the Dialog would allow only files with the .jpg extension to be displayed to the user.
What if you want the user to select multiple types image formats? It is similar to the above.
OpenDLG.Filter = "Images Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"
That will allow the user to select only bitmap, gif, and/or jpeg files.
Lets say you want the user to be able to do all 3 of the extensions, but only one Extension at any given time and allow them to pick from the box what extension they want? It works as outlined below…
OpenDLG.Filter = "Bitmap Files (*.bmp)|*.bmp|Gif Files (*.gif)|*.gif|JPeg Files (*.jpg)|*.jpg"
When you show that dialog, then the Combobox will have those 3 formats available as separate extension items and will allow your user to select which to use.
What if you want the user to see all files that have any kind of extensions? See below…
OpenDLG.Filter = "All Files (*.*)|*.*"
OK, what if you want one of the 3 extensions as outlined above to be the first extension that is selected in the dialogs Combobox? That is where another feature comes in: .FilterIndex. IF you wanted the 3rd extension in the box to be selected when the Dialog is open, then just do…
OpenDLG.FilterIndex = 3
The next useful feature is the .InitialDirectory property. IT is the directory path you want the user to be at when they first open the Dialogbox.
OpenDLG.InitialDirectory = "c:\mypath\path"
The next is .Multiselect. You simply tell the dialog whether the user can select more than 1 file or not.
You can also reset all properties to their default values using the .Reset method.
OpenDLG.Reset() '– will set the Dialogs properties to the default settings.
Then the next is .ShowDialog. As the name suggests, it will display the Open or Save Dialog window to the user.
Simply call it like this…
It will then show the dialog window.
But how do you know if the user pressed Open or if they pressed Cancel? Check out the code below…
DialogResult = OpenDLG.ShowDialog
DialogResult will contain the result after the Dialog has been closed. So in other words if the user clicked Cancel, then DialogResult would would have that data.
DialogResult = OpenDLG.ShowDialog() If DialogResult = Windows.Forms.DialogResult.OK Then MsgBox("User Pressed Open/OK!") ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then MsgBox("User canceled the action!") End If
After the user pressed Cancel or Open, it would throw one of the Messages depending on which was clicked.
One last feature is the Title feature. That is the text the user will see in the Dialogs titlebar when it opens.
OpenDLG.Title = "Here is MY Dialog!!"
Well I hope this brief tutorial was able to help you get an understanding of the File Dialog. Have fun!