OpenFileDialog.Filter not filtering properly except All files (*.*)?

Posted on 2006-04-13
Last Modified: 2012-06-27
I am writing an application in Visual Basic .NET 2003, and am having trouble with the filter for the OpenFileDialog.  When I run the app and run the form to bring up the OpenFileDialog, it will not show any files with a *.htm or *.html.  But, if I choose the "All Files", it shows everything.  How come, if I choose the "HTML Pages" filter, it does not work?  Here is my code snippet:

    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
        ' Declare OpenFileDialog object
        Dim objOpenFileDialog As New OpenFileDialog

        ' Set the OpenFileDialog properties
        With objOpenFileDialog
            .Filter = "HTML Pages (*.htm; *.html) | *.htm; *.html | All files (*.*) | *.*"
            .FilterIndex = 1
            .InitialDirectory = "C:\"
            .Title = "Open Webpage"
        End With

        ' Show the dialog
        If objOpenFileDialog.ShowDialog = DialogResult.OK Then
            ' If the Open button was clicked, then load the file name selected
            txtOpenFile.Text = objOpenFileDialog.FileName
        End If

        ' Cleanup
        objOpenFileDialog = Nothing
    End Sub
Question by:pepsi_cola
    LVL 44

    Accepted Solution

    Hi pepsi_cola,

    >             .Filter = "HTML Pages (*.htm; *.html) |*.htm;*.html | All files (*.*) | *.*"

    You had spaces preceding the *.htm and *.html so unless your files started with a space there would be no match

    Tim Cottee

    Author Comment

    Its amazing what a space can do to mess everything up.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now