Text to Speech: For C# and VB NET Students
We're going to display an Open File Dialog box when our Open File button is clicked. We'd like to limit the files that can be chosen to Word files, PDFs, and text files. There is a problem, though.
You can't just dump a Word document or a PDF straight into a text box. For a Word document, you need to reference the Microsoft.Office.Interop service, and perform some not-too-tricky code manipulation. For a PDF, it's quite difficult to open it up and grab just the text. The easiest way to do it is to use a third-party library. We'll use GemBox. Plain text files are easy enough to handle.
What all this means, though, is that we need get what type of file the user has selected, docx, pdf, or txt. Grab the ending from the filename and you can pass that file name to a function, depending on what it is. Let's see how this works.
First, locate the OpenFileDialog control in the Dialogs category of the Visual Studio toolbox. Double-click to add one to you project. The default name is a bit long and unwieldly, so change the Name property to ofd1. Now double-click the Open File button on your form to create a code stub. Set up some properties for the OpenFileDialog by adding the following (delete the semicolon on the end in VB Net):
ofd1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd1.Title = "Open File";
ofd1.Filter = "Text Files|*.txt|Word Files|*.docx|PDF Files|*.pdf";
You've met these lines before in a precious section, but they just set an initial directory, a title, and a filter to restrict the types of files that can be opened.
We can show the dialog box and check if the cancel button wasn't clicked. Add this if statement in C#:
if (ofd1.ShowDialog() != DialogResult.Cancel)
And this one in VB Net:
If ofd1.ShowDialog() <> DialogResult.Cancel
As the code for the if statement, we need to get the name of the file that was chosen.
Add this to your if statement:
string fileName = ofd1.FileName;
Dim fileName As String = ofd1.FileName
Next, we can grab the ending of the file name. There is a method of the IO.Path Class called GetExtension that can get you which type of file you opened. So you need to add a reference to the IO Class at the very top of your code. Add this in C#:
And this in VB:
Now add this line in C#:
string ending = Path.GetExtension(fileName);
And this one in VB Net:
Dim ending As String = Path.GetExtension(fileName)
The GetExtension Function/method is does what it says in the tin: gets the file extension from a file path. Our path is in filename, which we got from the dialog box.
Let's add some If Statements to check what ending we have. Add these in C#:
if (ending == ".txt")
} else if (ending == ".docx")
else if (ending == ".pdf")
And these in VB Net:
If ending = ".txt" Then
ElseIf ending = ".docx" Then
ElseIf ending = ".pdf" Then
Your Open File button code should look like this in C#:
And this in VB Net:
Try it out. Run your program and click your Open File button. When you see the dialog box, it will be in the Documents folder. If you have any text files in there, select one and click Open. You should see the message box appear saying "Text Files". (If you don't have any text files in the Documents folder, you'll have to navigate to where they are.) Now try to open a Word file. (Don't forget to change the file type in the dropdown list at the bottom. It will say Text Files because that's the first one we added in the Filter.) Finally, do the same for PDFs. You should find that a different message box is displayed, depending on which type a file you tried to open.
Instead of a message box, however, we'll call a Subroutine/method to open the file selected. We'll do text files, first, in the next lesson below. There's only three lines of code to write!