Page 137 - DCAP408_WEB_PROGRAMMING
P. 137
Unit 8: File I/O
We’ll now write some code to manipulate the properties of our new control. So do the following: Notes
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 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(ByVal sender As Object, _
ByVal e As System.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.
Task To access this dialog box, select Open from the File menu and then choose File.
Analyze.
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 program. 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 program 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()
LOVELY PROFESSIONAL UNIVERSITY 131