Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake an OpenFileDialog validate the user's file selection in Visual Basic 2008
DescriptionThis example shows how to make an OpenFileDialog validate the user's file selection in Visual Basic 2008.
Keywordsstring, extension method, URL encode, URL decode, Visual Basic 2008
CategoriesFiles 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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated