Wednesday, October 29, 2008

Thurs, Oct. 30: Yet more work with menus

Greetings and salutations, VB coders!

Today, before you begin work on your projects, I would like to be able to extend the reach of your menus. So, without any further ado --not that there was any previous ado-- let's open up the "Menus" project we made the other day, and add two controls to it.

The first control is called an "OpenFileDialog"; just drag it onto the bottom of your "Menus" form, next to the menustrip you placed there the other day. In its properties sheet, rename it "OpenSaysMe"

The other control is a "SaveFileDialog" --yup, Microsoft went all out making creative names for these controls -- drag it next to the OpenFileDialog, and rename it "SaveIt"

Once you've done that, you will want to get into the code to Open files by doubleclicking on the Open command in your menu list. Then, add in this code:

'THIS IS WHERE WE OPEN A FILE AND PUT THE STUFF INTO OUR TEXTBOX
'OpenSaysMe is the name of the FileOpenDialog
OpenSaysMe.Title = "Go Get 'em!"
'start inside the folder for your project
OpenSaysMe.InitialDirectory = saveFolder

OpenSaysMe.ShowDialog() 'open up the control to browse for files
Dim theFileIwant As String
theFileIwant = OpenSaysMe.FileName
'set the string to whatever file you chose

'make a filereader that will use the file you chose
Dim objReader As New StreamReader(theFileIwant)
MyWords.Text = objReader.ReadToEnd 'put the text of file into textbox
objReader.Close() 'stop reading

OK, once you've done that, make sure you save your work, then run the program to make sure you can actually use this to use the "Open" menu command to open up files and then put their contents into a textbox. Please be careful to open up only files with actual, words in them!

Cheers,
Mr. L

PS, If you run into any difficulties, the first thing I would look at would be the Import statement at the top of your code. If need be, please add this import line:

Imports System.IO

PPS There will be another post soon which coughs up the code for maiking the Save menu work too; be looking for it!

No comments: