Houston, Can You Read Me?
If something as simple as basic text-file operations is difficult in .NET, adding a file dialog really makes things exciting for the coder. For this example, I decided to use a file open dialog for reading in the student names. I even threw in a confirmation to ensure that I choose the right file. The file listings on a Pocket PC can be confusing and rather "compact." (I like the subtle approach of acting as if I’m sniffing the screen while holding it close to my eyes. This is less obvious than using bifocals, yet still allows me to read the text clearly.)
Let’s begin with a few variable definitions. We need to derive an OpenFileDialog() box, a resultant answer as DialogResult, and maybe another DialogResult? Huh? Why did I use two of those when I wrote this code four months ago? Once you decrypt the code, wouldn’t it be better if you added some comments? (I teach commenting to my students by giving them truly cryptic stuff and let them see why and where comments are important.)
Okay, this is clear, just dimension the variables needed:
Dim filechoice As New OpenFileDialog() Dim clicked As DialogResult = filechoice.ShowDialog() Dim confirmOpen As DialogResult
If the user clicks Cancel, we’ll exit the OpenFileDialog, and processing returns to where it was before this routine started:
If clicked = Windows.Forms.DialogResult.Cancel Then Return End If
Here’s the returned filename:
myfilename = filechoice.FileName
Make sure that the filename is legitimate:
If myfilename = "" Or myfilename Is Nothing Then MessageBox.Show("Invalid Filename", "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) Else
Next, make sure that the user really wants to open this file:
confirmOpen = MessageBox.Show("Is " & myfilename & " the file you want to work with?", _ "Just checking", MessageBoxButtons.YesNo, MessageBoxIcon.Hand, _ MessageBoxDefaultButton.Button2)
Now that we have a reasonably legitimate filename, confirmed by the user, let’s work to open it. Wait. If you’re a smart reader, you know of other filename issues that might create a problem:
- You might edit the code and add a few more validation tests.
- While you’re at it, you should restrain the user from opening system files.
- What other issues might a hacker use to take advantage of your code? (The goal here is to get you thinking about improvements, not foolishly accepting example code as perfect.)
Let’s begin with the activity that occurs if the user confirms the file selection:
If confirmOpen = Windows.Forms.DialogResult.Yes Then
As with the write operation, we open a FileStream (equivalent to our mode portion of the old Visual Basic’s file read statement):
myfile = New FileStream(myfilename, FileMode.Open, FileAccess.Read)
We derive a StreamReader from the open FileStream, and this StreamReader will be your "filehandle" throughout the rest of the processing:
myfilereader = New StreamReader(myfile)
As a conscientious coder, you want to disable interface elements that are no longer needed, while enabling interface elements needed for this stage of processing. (This keeps the user from making honest mistakes.) Here’s how we’ll do it:
Kid1.Text = " " Kid2.Text = " " Kid3.Text = " " Fopen.Enabled = False btnNext.Enabled = True Else Return End If
What’s an Fopen? It’s a button. Hungarian notation is out of vogue among many coders, but I sure miss buttons announcing themselves as btnButtonName. Um, is there a better way to .clear() that text?