The Open File Dialogue Box

The Open File Dialogue Box

For the last project, you designed a form interface that had a File, Edit and a View menu. In this section, we'll write code so that your menu items actually do something other than displaying message boxes. In other words, the Edit > Cut menu will really cut text, and the Edit > Paste menu will really paste text.

So open up the project you completed for the previous section. Comment out or delete any message box code. (You comment out code by typing a single quote character at the start of the line. The line will then turn green, and will be ignored when the programme is run.)

We'll start with the File > Open menu.

 

Open File Dialogue Box

In most programmes, if you click the File menu, and select the Open item, a dialogue box is displayed. From the dialogue box, you can click on a file to select it, then click the Open button. The file you clicked on is then opened up. We'll see how to do that from our menu. (Except, the file won't open yet - only the dialogue box will display, and then name of the chosen file. You'll learn how to open files in a later section.)

First, place two textboxes on your form. In the properties box, locate the MultiLine property. It is set to False by default (which is why you can't change the height of textboxes). Change this value to True.

Type some default text for the Text Property of textbox1. Change the Font size to 14 points.

Your form should now look something like this one:

Form with menu and two textboxes

We'll work with these textboxes when we do the Edit menu. So let's leave them for now.

When we click on File > Open from our menu, we want the Open dialogue box to appear. This is fairly straightforward in VB.NET. In fact there is even a control for it!

Open up your toolbox, and locate the control called "OpenFileDialog". You might have to scroll down to see it. But you're looking for this:

The Open File Dialog Control

Double click the control to add one to your project.

But notice that the control doesn't get added to your form. It gets added to the area at the bottom, next to your menu control:

he Object area in VB .NET showing an open file dialog  control

The shaded area surrounding the control means that it is selected. If you look on your right, you'll see the properties that you can use with the control.

Click on the Name property and change the name to openFD. When you change the name in the properties box, the name of the control at the bottom will change:

The Control has been renamed to openFD

We’ll now write some code to manipulate the properties of our new control. So do the following:

  • Access the code for your File > Open menu item. (To do this quickly, you can simply double click the Open item on your menu bar. Or press F7 (CTRL + ALT + 0 in version 2012) to access the Code View.)
  • Click the name of your menu item from the left drop down box at the top of the code
  • Then select the Click event from the drop down box to the right
  • Your empty code should be this (the code below has underscore characters added, so that it can fit on this page):

Private Sub mnuOpen_Click( sender As Object, e As EventArgs) _
Handles mnuOpen.Click

End Sub

With you cursor flashing between the two lines of your code, add the following:

openFD.ShowDialog()

When you typed a full stop after the openFD, you probably saw a list box appear. You can just double click the ShowDialog() item to add it to your code.

But this method of the OpenFileDialog control does what you'd expect it to do: Shows the dialogue box. You can even test it out right now. Press F5 to run your programme. Then click the Open item on your File menu. You should see an Open dialogue box display.

Return to the design environment, and we'll explore some more things you can do with this Dialogue box control.

 

The Initial Directory

You can set which directory the dialogue box should display when it appears. Instead of it displaying the contents of the "My Documents" folder, for example, you can have it display the contents of any folder. This done with the Initial Directory property. Amend your code to this:

openFD.InitialDirectory = "C:\"
openFD.ShowDialog()

Run your programme again, and see the results in action. You should see the contents of the "C" folder on your hard drive (if you root folder is called something else, change the code above).

 

The Title Property

By default, the dialogue box will display the word "Open" as a caption at the top of your dialogue box. You can change this with the Title property. Add the line in Bold to your code:

openFD.InitialDirectory = "C:\"
openFD.Title = "Open a Text File"
openFD.ShowDialog()

Run your code again, and Click File > Open from your menu. You should see this at the top of the Open dialogue box:

A Title added to an open file dialogue box

 

The Open File Dialogue Box Filter Property

In the previous section, we saw how to add an Open File Dialogue to our menus. We then saw how to add an Initial Directory and a Title property. In this section, we'll learn about the Filter property.

 

The Dialogue Box Filter Property

In most dialogue boxes, you can display a list of specific files that can be opened. These are displayed in the "Files of Type" drop down list. To do this in VB.NET, you access the Filter property. We'll restrict our users to only opening Text files, those that end in the extension ".txt".

The following code shows how to use the filter property:

openFD.InitialDirectory = "C:\"
openFD.Title = "Open a Text File"
openFD.Filter = "Text Files|*.txt"
openFD.ShowDialog()

Run your code. Click File > Open on your menu, and then click the arrow on the drop down box for "Files of Type". You should see this:

Filter box in Windows 7

If you examine yourOpen dialogue box, you should see only text files displayed (you'll still see folders). If you can't see any files at all, double click a folder and explore. You'll soon see something like this:

Open File dialogue box showing text files

To display files of more than one type, add a Pipe character between each filter. In the code below, two file types are specified, text files and Microsoft Word documents:

openFD.Filter = "Text Files|*.txt|Word Files|*.doc"

When the programme is run, you should be able to see two file types in the list:

File Filters in Windows 7

If you want to launch the Open File Dialog Box inside of your Documents folder, there is some special code for this (it's a bit for this page, though, and should be on one line):

openFD.InitialDirectory = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments )

After the equal sign, you need Environment.GetFolderPath. In between round brackets, you can then specify a folder with Environment.SpecialFolder. As soon as you type a dot after SpecialFolder you'll see a menu appear. This one:

Special Folder Options

These folders are ones that have already been set up on your computer by Microsoft. Select the special folder that you want as your initial directory. The Documents folder is actually called MyDocuments.

 

Select a File from the Open File Dialogue Box

You'll notice from the previous section that if you select a file and click the Open button, nothing happens. That's because the Open dialogue boxes doesn't actually open files! It only displays a list of files that CAN be opened, if you were clever enough to write the code. We'll be writing the code that does the opening (and the saving) in a later section. But you need to be able to get the name of the file. The Open Dialogue box has a property that returns the file name that was selected. Not surprisingly, it's called FileName:

OpenFD.FileName

However, this is a property that returns a value (a string value). The value is the name of a file. So you have to assign this value to something. We can assign it to a new variable:

Dim strFileName As String

strFileName = OpenFD.FileName

The value in the variable strFileName will then hold the name of the file selected. So change you code to this (new lines in bold):

Dim strFileName As String

openFD.InitialDirectory = "C:\"
openFD.Title = "Open a Text File"
openFD.Filter = "Text Files|*.txt"
openFD.ShowDialog()
strFileName = OpenFD.FileName

MessageBox.Show( strFileName )

Run your programme, and click your File > Open menu. Navigate to where you have some text files. Click one to select it. Then click the Open button. You should see the name of the file displayed in your message box:

The selected file has been returned

Notice that the location (the path) of the file is also displayed.

One thing you may have noticed is that if you select a file, then click the Cancel button, the message box still displays. But it will be blank, or just say OpenFileDialog1. In your code, you will only want to do something with a file if the Cancel button is NOT clicked.

 

You can test to see if it was clicked by assigning the openFD.ShowDialog() to an integer:

Dim DidWork As Integer = openFD.ShowDialog()

You can then test what is inside of the DidWork variable. If the cancel button is clicked, the result of the action is stored by VB.NET in this property:

DialogResult.Cancel

You can compare the two in an if statement:

If DidWork = DialogResult.Cancel Then

MessageBox.Show("Cancel Button Clicked")

Else

strFileName = openFD.FileName
MessageBox.Show(strFileName)

End If

(NOTE: On some versions of Visual Studio, you may have to put Windows.Forms before DialogResult.Cancel)

In the code above, you're only opening the file if the Open button was clicked. The code is a bit more complicated, but study it for a while and it will make sense!

In the next part, we'll take a look at how to code for the Save menu.