News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Make an OpenFileDialog validate the user's file selection

Started by nandagopal, Nov 17, 2008, 07:07 PM

Previous topic - Next topic

nandagopal

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