.

Filter files with the Open File Dialogue Box

The 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 (in bold) 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:
Only text files can be opened here
You can add a little bit extra to the description part of the filter, if you like. This will server a s a reminder of just what the extension is. Try amending the line to this:
openFD.Filter = "Text Files(*.txt)|*.txt"
When you run your code, you see this in the Files of Type area:
The file extension has been added
If you scroll across your Open 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:
Only text files are showing
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:
Word and Text files are now available

0 comments:

Search Here