Pages

Sunday, 8 July 2012

VB.NET Filter OpenFileDialog

This tutorial is going to teach you to code you a Filtered OpenFileDialog in VB.NET. Sometime we need to restrict user to choose limited File Format. To do this we have to use a Filter OpenFileDialoag. In example given below we Filtered OpenFileDialog with two Format Text and Microsoft Word files using Filter property of OpenFileDialog. Before to use the code given below you have to open your Toolbox, locate the control called "OpenFileDialog". Double click on it to add it to your project.

Double Click on it
Double Click on it


Public Sub myOpenFileDialog()

        OpenFileDialog1.InitialDirectory = "D:\"
        OpenFileDialog1.Title = "Open a Text File"
        OpenFileDialog1.Filter = "Text Files|*.txt"
        OpenFileDialog1.Filter = "Text Files|*.txt|Word Files|*.doc"
        OpenFileDialog1.ShowDialog()

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        Dim filename As String = OpenFileDialog1.FileName
        If filename = "" Then
            MsgBox("Clicked on Cancel Button")
        Else
            filename = OpenFileDialog1.FileName
            MsgBox(filename)
        End If

End Sub

No comments:

Post a Comment