|
|
Title | Make an OpenFileDialog validate the user's file selection in Visual Basic 2008 |
Description | This example shows how to make an OpenFileDialog validate the user's file selection in Visual Basic 2008. |
Keywords | string, extension method, URL encode, URL decode, Visual Basic 2008 |
Categories | Files and Directories |
|
|
This example uses an OpenFileDialog named ofdTextFile to let the user select a text file. When the user selects a file and tries to accept it in the dialog, the dialog raises its FileOk event. The following event handler checks to see if the file's name ends with ".txt." If the file is not a text file, the code displays a message box and sets thue event handler's e.Cancel parameter to True to tell the dialog not to accept this file. The user must either select a file that ends with ".txt" or cancel the dialog.
|
|
Private Sub ofdTextFile_FileOk(ByVal sender As _
System.Object, ByVal e As _
System.ComponentModel.CancelEventArgs) Handles _
ofdTextFile.FileOk
If ofdTextFile.FileName.ToLower().EndsWith(".txt") Then
e.Cancel = False
Else
MessageBox.Show( _
"Please select a text file", _
"Select Text File", _
MessageBoxButtons.OK, _
MessageBoxIcon.Hand)
e.Cancel = True
End If
End Sub
|
|
|
|
|
|